This file contains hidden or 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
| # https://gist.github.com/1185131 | |
| class Array | |
| def next; self[1..-1] end | |
| # returns a function that is the combination of a sequence of function calls | |
| # on some external arguments. the functions combined are the elements of self. | |
| def compose | |
| lambda do |*args| | |
| inject(*args) do |mem, fn| |
This file contains hidden or 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
| class Object | |
| def out; tap { |x| puts x.inspect } end | |
| ends | |
| def S(*args) | |
| f = args.shift.to_proc | |
| args.instance_eval(&f) | |
| end | |
| S(:join, :a, :b, :c).out # => "abc" |
This file contains hidden or 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
| class Hash | |
| def /(key) | |
| self[key] || {} | |
| end | |
| end | |
| h = {:foo => {:bar => {:baz => "BOOM"}}} | |
| h/:foo/:bar/:baz # => "BOOM" | |
| h/:foo/:bar/:not_existing # => nil |
This file contains hidden or 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
| def has_key_path?(h, path) | |
| !path.detect do |key| | |
| !(h.respond_to?(:has_key?) && h.has_key?(key) && true.tap { h = h[key] }) | |
| end | |
| end | |
| h = {:a => {:b => 1, :c => nil}} | |
| puts has_key_path?(h, [:a, :x]) # false | |
| puts has_key_path?(h, [:a, :b]) # true |
This file contains hidden or 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
| ;; Write a function which returns the last element in a sequence. | |
| ;: Special restrictions: last | |
| (= (__ [1 2 3 4 5]) 5) | |
| (= (__ '(5 4 3)) 3) | |
| (= (__ ["b" "c" "d"]) "d") |
This file contains hidden or 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
| ;; fronx's solution to Analyze a Tic-Tac-Toe Board | |
| ;; https://4clojure.com/problem/73 | |
| (fn [m] | |
| (first | |
| (filter | |
| (fn win? [player] | |
| (->> (let [idx [0 1 2]] | |
| (concat m ; rows | |
| (map (fn [i] (map #(% i) m)) idx) ; columns |
This file contains hidden or 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
| ;; fronx's solution to Happy numbers | |
| ;; https://4clojure.com/problem/86 | |
| (fn [n] | |
| (loop [seen #{} n n] | |
| (let [v (->> n | |
| str vec | |
| (map #(- (int %) 48)) | |
| (map #(* % %)) | |
| (reduce +))] |
This file contains hidden or 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
| BELIEVE IN YOUR FUCKING SELF. | |
| STAY UP ALL FUCKING NIGHT. | |
| WORK OUTSIDE OF YOUR FUCKING HABITS. | |
| KNOW WHEN TO FUCKING SPEAK UP. | |
| FUCKING COLLABORATE. | |
| DON'T FUCKING PROCRASTINATE. | |
| GET OVER YOUR FUCKING SELF. | |
| KEEP FUCKING LEARNING. | |
| FORM FUCKING FOLLOWS FUNCTION. | |
| A COMPUTER IS A LITE-BRITE FOR BAD FUCKING IDEAS. |
This file contains hidden or 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
| class Mastermind | |
| attr_reader :answer | |
| def initialize(answer) | |
| @answer = answer | |
| end | |
| def match(picks) | |
| clues = [] | |
| picks.each_with_index do |pick, p_index| |
This file contains hidden or 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
| ;; fronx's solution to A Half-Truth | |
| ;; https://4clojure.com/problem/83 | |
| #(or (and (some false? %&) (some true? %&)) | |
| false) |