-
Notifications
You must be signed in to change notification settings - Fork 270
Introduction to higher order functions guide #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
||
A higher order function is a function that either; | ||
|
||
1. Takes one of more functions as arguments |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one or more?
;; => "Hello there Jake!" | ||
|
||
(greet-dog "Sparky") | ||
;; => "Hello there Sparky!" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the result be 'Woof woof Sparky!' ?
Accidentally add . instead of single file
Will attach feedback file to PR
Got feedback from a colleague of mine via a word doc. I attached the file for reference. |
- add note about closures - change language to first class citizens from functions are values - add clojure example to start
I made a bunch of updates based on the response from the Clojure subreddit. |
The comparison in the SCIP approach is a great teaching tool, I think. I like the use of it with the introduction of higher order functions.
I wanted to use the comparison approach here as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, surely lots more that could be included here with things like comp, juxt, every-prod, some-fn, etc, but this is a good beginning.
|
||
It's common to see function definitions in Clojure using `defn` like `(defn foo ...)`. | ||
However, this is just syntactic sugar for `(def foo (fn ...))` | ||
`fn`, and implicitly `defn`, yield a function object as its value that can be passed around like any other Clojure object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defn
does not yield a function object (it returns a var which refers to a function object)
1. Takes one or more functions as arguments | ||
2. Returns a function as its result | ||
|
||
This will turn out to be a important concept in functional programming in any language. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"will turn out to be a" -> "is an"
|
||
Higher order functions allow us to _compose_ functions. | ||
This means we can write small functions and combine them to create larger functions. | ||
Like putting a bunch of small lego bricks together to build a house. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lego => LEGO
== Anonymous Functions | ||
|
||
An anonymous function is a function without a name. | ||
In Clojure these can be defined in two ways, `fn` and the literal `#(...)`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be a little more careful in this section. fn
does (optionally) take a name, and this is sometimes a useful technique (as the name ends up in function class, which ends up in your stack trace) or to allow for recursive self-calls.
Maybe the entire section would be better named "Function Literals".
(def rock-bands (filter #(= :rock (:genre %)) bands)) | ||
---- | ||
|
||
The function literal supports multiple arguments via `%1`, `%2`, and so on. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is discussed elsewhere (the weird characters guide), but does not mention %
and %&
. Maybe instead of "and so on" this should actually finish the rest of that thought.
---- | ||
|
||
The returned function form `adder` is a closure. | ||
This means, it can access all of the variables that we're in scope when the function was created. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we're => were
---- | ||
|
||
We want to filter out the dogs because we're writing enterprise grade software. | ||
First, let's look at a normal for loop. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove "for" (this is just a loop)
---- | ||
|
||
This code works fine, but it's bulky and confusing. | ||
We can simplpify this using `filter`, a higher order function. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
simplpify => simplify
]) | ||
---- | ||
|
||
We want to filter out the dogs because we're writing enterprise grade software. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, the "out" implies removal to me, but you want to keep the dogs. Maybe replace "filter out" with "keep only".
Higher order functions aren't always the easiest thing to get a hold of for a beginner and there isn't anything on the official site that I've found yet, so I prioritized this one.
Refers to issue 131 from a while back.
Trying to get some of this in before the Conj.