-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filtering a sequence so it only returns the elements that return true when applying the given function. I nice continuation from the sequence map exercise - number 17 in 4Clojure.
- Loading branch information
1 parent
7ff29a9
commit f75d84c
Showing
1 changed file
with
40 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
(ns four-clojure.018-sequences-filter) | ||
|
||
;; #18 - Sequences: filter | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
;; Difficulty: Elementary | ||
;; Topics: | ||
|
||
;; The filter function takes two arguments: a predicate function (f) and a sequence (s). Filter returns a new sequence consisting of all the items of s for which (f item) returns true. | ||
|
||
;; (= __ (filter #(> % 5) '(3 4 5 6 7))) | ||
|
||
|
||
;; Deconstruct the problem | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
;; filter works in a similar way to the `map` function in that it uses a function over the elements of a collection. | ||
|
||
;; The `filter` function only returns the values in the collection that return true from the given function. | ||
|
||
(filter odd? [1 2 3 4 5]) | ||
;; => (1 3 5) | ||
|
||
;; The function used in the 4Clojure test checks to see if a value is greater than 5 | ||
;; in its full form, the function is | ||
(fn [arg] | ||
(> arg 5)) | ||
|
||
;; in the 4Clojure test, it uses the short form | ||
#(> % 5) | ||
|
||
;; So when we filter the collection with this function only values greater than 5 are returned | ||
(filter #(> % 5) '(3 4 5 6 7)) | ||
;; => (6 7) | ||
|
||
|
||
;; Answers summary | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
(6 7) |