IF, WHEN or SOME

272 views
Skip to first unread message

Stephen Feyrer

unread,
Dec 11, 2017, 6:56:29 PM12/11/17
to clo...@googlegroups.com
Hi there,

I have been trying to shake this thought for a while now.  Essentially, my thought was if you can return a function why not decision component of an IF, WHEN or SOME statement?  That would give you a re-usable named choice.

Then you could write:

(celebration: do-something do-something-else)


This would be equivalent to writing:

(def success [apples bananas pears])

(defn celebration: [x y] (if (empty? success) x y))

(celebration: (do-something do-something-else))


I'm reasonably certain of the foolishness of this thought but occasionally, I have doubts.

Maybe I'm barking up the wrong tree or possibly I've seen something like this before and forgotten about it.  Perhaps, this is just taking things too far...  Either way, it's deferring the choice until it's needed.  In the right hands it could make for more readable code.

For completeness sake, to define the first form above you'd use:

(defc celebration: (if (empty? success)))


A more usable example might look like:

(def nums [1 2 3 4 5 6 7 8])

(defc even-nums: (some (even? nums)))

I guess this makes the real question, is it a good thing to be able to defer choice like this?


Btw, defc would be like def-choice but other options might be deft - def-test or defp - def-predicate.


--
Kind regards

Stephen

Timothy Baldridge

unread,
Dec 11, 2017, 6:58:20 PM12/11/17
to clo...@googlegroups.com
I talked a bit about this in my video on Boolean Blindness: https://www.youtube.com/watch?v=K1LaaJMscCc

Might be worth a watch. 

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
“One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.”
(Robert Firth)

Stephen Feyrer

unread,
Dec 12, 2017, 2:53:01 AM12/12/17
to clo...@googlegroups.com
Hi Tim,

Thank you.


--
Kind regards

Stephen.

Stephen Feyrer

unread,
Sep 20, 2018, 11:44:41 AM9/20/18
to clo...@googlegroups.com
Hi,

I have just been playing around with this idea and I got something.

user=> (def some-numbers ‘(2 4 6 8))  #This is my value to test later.
#’user/some-numbers
user=> (def evens? (partial (when (apply = (map even? some-numbers)))))
#’user/evens?
user=> (evens? (println “one”))
one
NullPointerException   user/eval239 (NO_SOURCE_FILE:74)
user=> 

What is my mistake?

Thanks.

Orestis Markou

unread,
Sep 20, 2018, 12:06:15 PM9/20/18
to clo...@googlegroups.com
In addition, when is a macro, so I don’t think you can call partial on it :)

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.

Orestis Markou

unread,
Sep 20, 2018, 12:06:16 PM9/20/18
to clo...@googlegroups.com
evens? is not a macro, therefore when you do (evens? (println “one”)), the println will be evaluated first, and its return value, nil, gets passed into the evens function. 



20 Σεπ 2018, 5:44 μμ, ο χρήστης «Stephen Feyrer <stephen...@gmail.com>» έγραψε:


For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.

Alan Thompson

unread,
Sep 20, 2018, 4:48:20 PM9/20/18
to clojure
`println` always returns `nil`.  So it prints "one", returns `nil`, and you try to execute the form:

(nil)  => NullPointerException

user=> (evens? (println “one”))
one
NullPointerException   user/eval239 (NO_SOURCE_FILE:74)

clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
“One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.”
(Robert Firth)

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

Alan Thompson

unread,
Sep 20, 2018, 4:50:01 PM9/20/18
to clojure
Should have been:    (evens? nil)

Paul Rutledge

unread,
Sep 20, 2018, 7:39:47 PM9/20/18
to Clojure
I haven't used them much myself but the idea of "reified control flow" is called a continuation. Some other lisps implement continuations, Clojure does not. I'm sure there's plenty of material out there about why Clojure/Rich did not provide continuations.

Carlo Zancanaro

unread,
Sep 20, 2018, 8:11:18 PM9/20/18
to clo...@googlegroups.com
Hey Stephen,

On Fri, Sep 21 2018, Stephen Feyrer wrote:
> user=> (def some-numbers ‘(2 4 6 8)) #This is my value to test
> later.
> #’user/some-numbers

At this point we have some-numbers = '(2 4 6 8)

> user=> (def evens? (partial (when (apply = (map even?
> some-numbers)))))
> #’user/evens?

Let's work through this:

(map even? '(2 4 6 8)) = '(true true true true)
(apply = '(true true true true)) = true
(when true) = nil
(partial nil) = nil

This means that we have evens? = nil

> user=> (evens? (println “one”))
> one
> NullPointerException user/eval239 (NO_SOURCE_FILE:74)

So now we have:

(println "one") = nil (with the side effect of printing "one")
(nil nil) throws NullPointerException

Clojure is attempting to call nil as a function, which throws a
NullPointerException.

I hope that helps! I'm not sure what you're actually trying to do,
so it's hard for me to give advice on how to do it.

Carlo
Reply all
Reply to author
Forward
0 new messages