Skip to content

Instantly share code, notes, and snippets.

@charliegriefer
Last active June 2, 2017 06:03
Show Gist options
  • Select an option

  • Save charliegriefer/1c8552ac9676ad247691cbec3bf6920c to your computer and use it in GitHub Desktop.

Select an option

Save charliegriefer/1c8552ac9676ad247691cbec3bf6920c to your computer and use it in GitHub Desktop.
The Elegance of Clojure
;; For a given list with N integers, return a new list removing the elements at odd positions.
;; Source: https://www.hackerrank.com/challenges/fp-filter-positions-in-a-list
;; My solution:
(fn[lst]
(map
#(when ((comp odd? first) %)
(println (second %)))
(map-indexed list lst)))
;; A _much_ more elegant solution:
(fn [lst]
(map second (partition 2 lst)))
@charliegriefer
Copy link
Author

charliegriefer commented Jun 1, 2017

Just had a shower thought. Literally, came to me as I was shampooing my beard.

(fn [lst] 
    (rest (take-nth 2 (conj coll 0))))  



user=> (rest (take-nth 2 (conj '(8 15 22 1 10 6 2 18 18 1) 0)))
;; => (15 1 6 18 1)

I'll break it down later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment