forked from functional-koans/clojure-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some examples for threading macros and a couple of simple transdu…
…cer koans
- Loading branch information
Showing
3 changed files
with
112 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
(ns koans.25-threading-macros | ||
(:require [koan-engine.core :refer :all])) | ||
|
||
(def a-list | ||
'(1 2 3 4 5)) | ||
|
||
(def a-list-with-maps | ||
'({:a 1} {:a 2} {:a 3})) | ||
|
||
(defn function-that-takes-a-map [m a b] | ||
(do | ||
(println (str "Other unused arguments: " a " " b)) | ||
(get m :a))) | ||
|
||
(defn function-that-takes-a-coll [a b coll] | ||
(do | ||
(println (str "Other unused arguments: " a " " b)) | ||
(map :a coll))) | ||
|
||
(meditations | ||
"We can use thread first for more readable sequential operations" | ||
(= __ | ||
(-> {} | ||
(assoc :a 1))) | ||
|
||
"Consider also the case of strings" | ||
(= __ | ||
(-> "Hello world" | ||
(str ", and moon") | ||
(str ", and stars"))) | ||
|
||
"When a function has no arguments to partially apply, just reference it" | ||
(= __ | ||
(-> "String with a trailing space " | ||
clojure.string/trim)) | ||
|
||
"Most operations that take a scalar value as an argument can be threaded-first" | ||
(= __ | ||
(-> {} | ||
(assoc :a 1) | ||
(assoc :b 2) | ||
(assoc :c {:d 4 | ||
:e 5}) | ||
(update-in [:c :e] inc) | ||
(get-in [:c :e]))) | ||
|
||
"We can use functions we have written ourselves that follow this pattern" | ||
(= __ | ||
(-> {} | ||
(assoc :a 1) | ||
(function-that-takes-a-map "hello" "there"))) | ||
|
||
"We can also thread last using ->>" | ||
(= __ | ||
(->> [1 2 3] | ||
(map inc))) | ||
|
||
"Most operations that take a collection can be threaded-last" | ||
(= __ | ||
(->> a-list | ||
(map inc) | ||
(filter even?) | ||
(into []) | ||
(reduce +))) | ||
|
||
"We can use funtions we have written ourselves that follow this pattern" | ||
(= __ | ||
(->> a-list-with-maps | ||
(function-that-takes-a-coll "hello" "there") | ||
(into [])))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
(ns koans.26-transducers | ||
(:require [koan-engine.core :refer :all])) | ||
|
||
(def xfms | ||
(comp (map inc) | ||
(filter even?))) | ||
|
||
(meditations | ||
"Consider that sequence operations can be used as transducers" | ||
(= __ | ||
(transduce xfms conj [1 2 3])) | ||
|
||
"We can do this eagerly" | ||
(= __ | ||
(into [] xfms [1 2 3])) | ||
|
||
"Or lazily" | ||
(= __ | ||
(sequence xfms [1 2 3])) | ||
|
||
"The transduce function can combine mapping and reduction" | ||
(= __ | ||
(transduce xfms + [1 2 3]))) |