I'm getting my feet wet with lisp and came across an (I think) unusual problem. I'd like to create very long lists; ie, something like (setf *mat* (make-list 1000000)), but without having Nil printed out a million times on the screen.
The best I came up with is...
(let () (setf *mat* (make-list 1000000)) (length *mat*))
(or some other short but useless function at the end of the closure)
...but I suspect there is a better solution to avoid these sesquipedalian printouts. Any input is appreciated. Btw, I'm using Clozure v1.10 under Windows 7.
Usually one would call (values) at the end.
Common Lisp has a way to deal with long output at the printer level:
Welcome to Clozure Common Lisp Version 1.9-dev-r15612M-trunk (DarwinX8664)!
? *print-length*
NIL
? (setf *print-length* 100)
100
? (make-list 1000000)
(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL
NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL
NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL
NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL
NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL
NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL
NIL NIL NIL NIL ...)
*print-length* here is the variable which controls it.
An alternative to setting *print-length* is to use defparameter instead of setf on the repl. defparameter returns the symbol instead of the value:
(defparameter *mat* (make-list 10000))
-> *mat*
Related
This question already has answers here:
What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?
(16 answers)
Closed 4 years ago.
#IBAction func login(_ sender: Any) {
guard emailField.text != "", passField.text != "" else {return}
Auth.auth().signIn(withEmail:emailField.text!,password:passField.text!, completion: { (user, error) in
if let error = error {
print(error.localizedDescription)
I get this error:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value.
Try:
guard let email = self.emailField.text, let password = self.passField.text else { return }
if email != "" && pass != "" {
Auth.auth().signIn(withEmail: email, password: password) { (user, err) in
if err != nil { print(err.localizedDescription); return }
// do something
}
}
I am parsing JSON data. After I start getting unexpectedly found nil while unwrapping an Optional value errors. I tried to use guard statement.
But again I am getting the same error.
guard let articleTitle = self.articles?[indexPath.row]["title"].string! else {return}
I simulate nil value like this:
guard let articleTitle = self.articles?[indexPath.row]["t"].string! else {return}
What I am doing wrong?
It doesn’t make much sense to force unwrap the optional in a conditional let assingment. Remove the !:
guard let articleTitle = self.articles?[indexPath.row]["title"].string else {return}
Otherwise the right-hand side will never produce nil but crash.
When I open one emacs, everything works fine.
But when I open second emacs on different terminal,
error occurs
Debugger entered--Lisp error: (invalid-read-syntax "#")
signal(invalid-read-syntax ("#"))
(condition-case errvar (progn (load savehist-file nil (not (interactive-p))) (setq savehist-loaded t)) (error (setq savehist-mode nil) (savehist-uninstall) (signal (car errvar) (cdr errvar))))
(progn (condition-case errvar (progn (load savehist-file nil (not (interactive-p))) (setq savehist-loaded t)) (error (setq savehist-mode nil) (savehist-uninstall) (signal (car errvar) (cdr errvar)))))
(if (and (not savehist-loaded) (file-exists-p savehist-file)) (progn (condition-case errvar (progn (load savehist-file nil (not (interactive-p))) (setq savehist-loaded t)) (error (setq savehist-mode nil) (savehist-uninstall) (signal (car errvar) (cdr errvar))))))
(if (not savehist-mode) (savehist-uninstall) (if (and (not savehist-loaded) (file-exists-p savehist-file)) (progn (condition-case errvar (progn (load savehist-file nil (not (interactive-p))) (setq savehist-loaded t)) (error (setq savehist-mode nil) (savehist-uninstall)$
savehist-mode(1)
eval-buffer(#<buffer *load*> nil "/home/eugenekim/.emacs.d/init.el" nil t) ; Reading at buffer position 21294
load-with-code-conversion("/home/eugenekim/.emacs.d/init.el" "/home/eugenekim/.emacs.d/init.el" t t)
load("/home/eugenekim/.emacs.d/init" t t)
#[0 "^H\205\266^# \306=\203^Q^#\307^H\310Q\202?^# \311=\204^^^#\307^H\312Q\202?^#\313\307\314\315#\203*^#\316\202?^#\313\307\314\317#\203>^#\320\321\322!D\nB^R\323\202?^#\316\324^S\325^A\324\211#\210^K\324=\203e^#\326\327\330\307^H\331Q!\"\325^A\324\211#\210^K\324=\203$
command-line()
normal-top-level()
I tracked down it's # in ./emacs.d/history file, everything works fine if I delete lines with # in the file, but I'd like to fix it once and for all :(
It seems # in (setq minibuffer-history ..) is the problem.
(setq minibuffer-history '("/home/eugenekim/Documents/zibann/momsite/momsite/conf/dev/webpack.config.js" #("var" 0 3 (fontified t font-lock-face font-lock-keyword-face)) "review-meta-review" "review main thread view" "11" "webpack" "/home/eugenekim/Documents/zibann-" "in\
it" #("5-static-elements" 0 17 (dired-filename t mouse-face highlight help-echo "mouse-2: visit this file in other window" fontified t)) "/home/eugenekim/tmp/react-grid-layout/test/ex" "we" "/home/eugenekim/tmp/react-grid-layout/" "inhouse webpack" "webp" "/home/eugeneki\
m/tmp/react-grid-layout/" "packa" #("To" 0 2 (magit-section [cl-struct-magit-section process #<process git> #<marker (moves after insertion) at 18819 in *magit-process: zibann> #<marker at 18869 in *magit-process: zibann> #<marker at 19136 in *magit-process: zibann> nil \
nil nil nil nil #<process git> [cl-struct-magit-section processbuf nil #<marker (moves after insertion) at 19302 in *magit-process: zibann> nil #<marker at 19303 in *magit-process: zibann> nil nil nil nil nil nil nil ([cl-struct-magit-section process nil #<marker (moves \
after insertion) at 12596 in *magit-process: zibann> nil #<marker at 12615 in *magit-process: zibann> nil nil nil nil nil nil #6 nil] [cl-struct-magit-section process #<process git> #<marker (moves after insertion) at 12615 in *magit-process: zibann> #<marker at 12635 in\
*magit-process: zibann> #<marker at 12729 in *magit-process: zibann> t nil nil nil nil #<process git> #6 nil] [cl-struct-magit-section process #<process git> #<marker (moves after insertion) at 12729 in *magit-process: zibann> #<marker at 12781 in *magit-process: zibann\
> #<marker at 13055 in *magit-process: zibann> t nil nil nil nil #<process git> #6 nil] [cl-struct-magit-section process nil #<marker (moves after insertion) at 13055 in *magit-process: zibann> #<marker at 13094 in *magit-process: zibann> #<marker at 13179 in *magit-proc\
ess: zibann> t nil nil nil nil nil #6 nil] [cl-struct-magit-section process nil #<marker (moves after insertion) at 13179 in *magit-process: zibann> #<marker at 13219 in *magit-process: zibann> #<marker at 16090 in *magit-process: zibann> t nil nil nil nil nil #6 nil] [c\
l-struct-magit-section process nil #<marker (moves after insertion) at 16090 in *magit-process: zibann> nil #<marker at 16146 in *magit-process: zibann> nil nil nil nil nil nil #6 nil] [cl-struct-magit-section process nil #<marker (moves after insertion) at 16146 in *mag\
it-process: zibann> nil #<marker at 16973 in *magit-process: zibann> nil nil nil nil nil nil #6 nil] [cl-struct-magit-section process nil #<marker (moves after insertion) at 16973 in *magit-process: zibann> nil #<marker at 16992 in *magit-process: zibann> nil nil nil nil\
nil nil #6 nil] [cl-struct-magit-section process #<process git> #<marker (moves after insertion) at 16992 in *magit-process: zibann> #<marker at 17012 in *magit-process: zibann> #<marker at 18239 in *magit-process: zibann> t nil nil nil nil #<process git> #6 nil] [cl-st\
ruct-magit-section process nil #<marker (moves after insertion) at 18239 in *magit-process: zibann> nil #<marker at 18340 in *magit-process: zibann> nil nil nil nil nil nil #6 nil] [cl-struct-magit-section process #<process git> #<marker (moves after insertion) at 18340 \
in *magit-process: zibann> #<marker at 18360 in *magit-process: zibann> #<marker at 18449 in *magit-process: zibann> t nil nil nil nil #<process git> #6 nil] [cl-struct-magit-section process nil #<marker (moves after insertion) at 18449 in *magit-process: zibann> #<marke\
r at 18475 in *magit-process: zibann> #<marker at 18534 in *magit-process: zibann> t nil nil nil nil nil #6 nil] [cl-struct-magit-section process nil #<marker (moves after insertion) at 18534 in *magit-process: zibann> #<marker at 18568 in *magit-process: zibann> #<marke\
r at 18819 in *magit-process: zibann> t nil nil nil nil nil #6 nil] #5 [cl-struct-magit-section process nil #<marker (moves after insertion) at 19136 in *magit-process: zibann> #<marker at 19175 in *magit-process: zibann> #<marker at 19302 in *magit-process: zibann> nil \
nil nil nil nil nil #6 nil])] nil])) "/home/eugenekim/tmp/react-grid-layout/" "/home/eugenekim/tmp/react-grid-layout/package.json" "/home/eugenekim/tmp/grid" "/home/eugenekim/Documents/zibann/momsite/node_modules/react-g" "magit" "webpack" "magit:" "/home/eugenekim/Docum\
ents/zibann/momsite/apps/reservation/static/reservation/calendar/view_configs/calendar-day.react.js" "/home/eugenekim/Documents/zibann/momsite/apps/reservation/static/reservation/calendar/view_configs/month" "/home/eugenekim/Documents/zibann/momsite/apps/reservation/stat\
ic/reservation/calendar/view_configs/" "calendar view config react js" #("stash" 0 5 (face magit-hash magit-section [cl-struct-magit-section stash "stash#{8}" #<marker (moves after insertion) at 749 in *magit: zibann> nil #<marker at 1 in *magit: zibann> nil nil nil nil \
nil nil [cl-struct-magit-section stashes "refs/stash" #<marker (moves after insertion) at 749 in *magit: zibann> #<marker at 1 in *magit: zibann> #<marker at 1 in *magit: zibann> nil nil nil nil nil nil [cl-struct-magit-section status nil #<marker (moves after insertion)\
at 749 in *magit: zibann> nil #<marker at 1 in *magit: zibann> nil nil nil nil nil nil nil ([cl-struct-magit-section branch "feature/reservation" #<marker (moves after insertion) at 749 in *magit: zibann> #<marker at 1 in *magit: zibann> #<marker at 1 in *magit: zibann>\
nil nil nil nil nil nil #7 ([cl-struct-magit-section branch #("origin/feature/reservation" 0 26 (face magit-branch-remote)) #<marker (moves after insertion) at 749 in *magit: zibann> nil #<marker at 1 in *magit: zibann> nil nil nil nil nil nil #9 nil] [cl-struct-magit-s\
ection tag "saleor" #<marker (moves after insertion) at 749 in *magit: zibann> nil #<marker at 1 in *magit: zibann> nil nil nil nil nil nil #9 nil])] #6 [cl-struct-magit-section unpushed "#{upstream}.." #<marker (moves after insertion) at 749 in *magit: zibann> #<marker \
at 1 in *magit: zibann> #<marker at 1 in *magit: zibann> nil nil nil nil nil nil #7 ([cl-struct-magit-section commit "e7b4341" #<marker (moves after insertion) at 749 in *magit: zibann> nil #<marker at 1 in *magit: zibann> nil nil nil nil nil nil #9 nil])])] ([cl-struct-\
magit-section stash "stash#{0}" #<marker (moves after insertion) at 749 in *magit: zibann> nil #<marker at 1 in *magit: zibann> nil nil nil nil nil nil #6 nil] [cl-struct-magit-section stash "stash#{1}" #<marker (moves after insertion) at 749 in *magit: zibann> nil #<mar\
ker at 1 in *magit: zibann> nil nil nil nil nil nil #6 nil] [cl-struct-magit-section stash "stash#{2}" #<marker (moves after insertion) at 749 in *magit: zibann> nil #<marker at 1 in *magit: zibann> nil nil nil nil nil nil #6 nil] [cl-struct-magit-section stash "stash#{3\
}" #<marker (moves after insertion) at 749 in *magit: zibann> nil #<marker at 1 in *magit: zibann> nil nil nil nil nil nil #6 nil] [cl-struct-magit-section stash "stash#{4}" #<marker (moves after insertion) at 749 in *magit: zibann> nil #<marker at 1 in *magit: zibann> n\
il nil nil nil nil nil #6 nil] [cl-struct-magit-section stash "stash#{5}" #<marker (moves after insertion) at 749 in *magit: zibann> nil #<marker at 1 in *magit: zibann> nil nil nil nil nil nil #6 nil] [cl-struct-magit-section stash "stash#{6}" #<marker (moves after inse\
rtion) at 749 in *magit: zibann> nil #<marker at 1 in *magit: zibann> nil nil nil nil nil nil #6 nil] [cl-struct-magit-section stash "stash#{7}" #<marker (moves after insertion) at 749 in *magit: zibann> nil #<marker at 1 in *magit: zibann> nil nil nil nil nil nil #6 nil\
] #5 [cl-struct-magit-section stash "stash#{9}" #<marker (moves after insertion) at 749 in *magit: zibann> nil #<marker at 1 in *magit: zibann> nil nil nil nil nil nil #6 nil] [cl-struct-magit-section stash "stash#{10}" #<marker (moves after insertion) at 749 in *magit: \
zibann> nil #<marker at 1 in *magit: zibann> nil nil nil nil nil nil #6 nil] [cl-struct-magit-section stash "stash#{11}" #<marker (moves after insertion) at 749 in *magit: zibann> nil #<marker at 1 in *magit: zibann> nil nil nil nil nil nil #6 nil] [cl-struct-magit-secti\
on stash "stash#{12}" #<marker (moves after insertion) at 749 in *magit: zibann> nil #<marker at 1 in *magit: zibann> nil nil nil nil nil nil #6 nil] [cl-struct-magit-section stash "stash#{13}" #<marker (moves after insertion) at 749 in *magit: zibann> nil #<marker at 1 \
in *magit: zibann> nil nil nil nil nil nil #6 nil] [cl-struct-magit-section stash "stash#{14}" #<marker (moves after insertion) at 749 in *magit: zibann> nil #<marker at 1 in *magit: zibann> nil nil nil nil nil nil #6 nil])] nil] keymap (keymap (65 . magit-stash-pop) (97\
. magit-stash-apply) (remap keymap (magit-delete-thing . magit-stash-drop) (magit-visit-thing . magit-stash-show))))) "magit:" "calendar util" "reduc" "calendar contain" "toolbar" "gud" "actions" "toolbar" "month" "day" "dates" "web" "view" "py bun" "py" "dates" "viewse\
t" "gud" "produ" "gud" "actions" "day." "js" "/home/eugenekim/Documents/zibann/momsite/apps/reservation/" "reservation calendar" "gud" "bundle" "day" "dates" "gud" "calendar" "day." "calendar " "month" "gud" "py" "month" "gud" "day" "/home/eugenekim/Documents/zibann/moms\
ite/apps/reservation/static/reservation/calendar/actions/" "actions" "base" #("product" 0 7 (fontified t font-lock-face font-lock-string-face)) "/home/eugenekim/Documents/zibann/momsite/apps/saleor/product/rest/viewsets/product/bundle_product/bundle_product.py" "regul" "\
gud" #("product" 0 7 (fontified t font-lock-face font-lock-string-face)) ".js view" "produc" "product py" "/home/eugenekim/Documents/zibann/momsite/apps/reservation/static/reservation/calendar/reducers/" "reducer" "day" "gud" "actions" "mon" "produc py" "js" "month" "dat\
es" "month" "actions" "month" "day" "/home/eugenekim/Documents/zibann/momsite/apps/reservation/static/reservation/calendar/reducers/ind" "/home/eugenekim/Documents/zibann/momsite/apps/reservation/static/reservation/calendar/reducers/dates.react.js" "/home/eugenekim/Docum\
ents/zibann/momsite/apps/reservation/static/reservation/calendar/reducers/" "days" "calendar" "day." "actions"))
I am new in Swift. My question is I am not sure how to unwrapping the optional value. When I print the object.objectForKey("profile_picture"), I can see Optional(<PFFile: 0x7fb3fd8344d0>).
let userQuery = PFUser.query()
//first_name is unique in Parse. So, I expect there is only 1 object I can find.
userQuery?.whereKey("first_name", equalTo: currentUser)
userQuery?.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in
if error != nil {
}
for object in objects! {
if object.objectForKey("profile_picture") != nil {
print(object.objectForKey("profile_picture"))
self.userProfilePicture.image = UIImage(data: object.objectForKey("profile_pricture")! as! NSData)
}
}
})
You'd use if let to perform "optional binding", only performing the block if the result in question is not nil (and binding the variable profilePicture to the unwrapped value in the process).
It would be something like:
userQuery?.findObjectsInBackgroundWithBlock { objects, error in
guard error == nil && objects != nil else {
print(error)
return
}
for object in objects! {
if let profilePicture = object.objectForKey("profile_picture") as? PFFile {
print(profilePicture)
do {
let data = try profilePicture.getData()
self.userProfilePicture.image = UIImage(data: data)
} catch let imageDataError {
print(imageDataError)
}
}
}
}
Or, if you want to get data asynchronously, perhaps:
userQuery?.findObjectsInBackgroundWithBlock { objects, error in
guard error == nil && objects != nil else {
print(error)
return
}
for object in objects! {
if let profilePicture = object.objectForKey("profile_picture") as? PFFile {
profilePicture.getDataInBackgroundWithBlock { data, error in
guard data != nil && error == nil else {
print(error)
return
}
self.userProfilePicture.image = UIImage(data: data!)
}
}
}
}
It would be something along those lines, using if let to unwrap that optional. And you then have to get the NSData associated with that the PFFile object (from the getData method or getDataInBackgroundWithBlock, presumably).
See Optional Binding discussion in The Swift Programming Language.
I have this function that uses guard to make sure the parameters are correct:
func foo(bundle: NSBundle?, _ url: NSURL?)->Bool{
guard let _ = url, let _ = bundle else{
print("crap")
return false
}
return true
}
I noticed that the else clause will run if ANY of the parameters is nil. It behaves as an OR clause.
However, what I need is to check that BOTH are nil (an AND).
How can I do this?
If you don't actually care about your bound values (which I assume you don't, given the _ names), you can do this:
func foo(bundle: NSBundle?, _ url: NSURL?) -> Bool {
guard url != nil || bundle != nil else {
print("crap")
return false
}
return true
}
If you do actually want to bind the values, then you cannot accomplish this with a single guard statement. If it fails to unwrap a value from either of the optionals then it will execute the else block, as you've noted.
If you were still set on using a guard statement, you would have to put in a guard as I have above followed by various if let statements to unwrap things as appropriate (or just omit the guard and have an else statement in the code below that was the false case):
func foo(bundle: NSBundle?, _ url: NSURL?) -> Bool {
guard url != nil || bundle != nil else {
print("crap")
return false
}
if url == nil, let someBundle = bundle {
// Handle the bundle only case
}
else if bundle == nil, let someUrl = url {
// Handle the url only case
}
else if let someUrl = url, let someBundle = bundle {
// Handle the case for both
}
return true
}
This is not really a job for a guard statement. What you want is:
if(url == nil && bundle == nil)
{
print("crap")
return false
}
return true