Skip to content

Instantly share code, notes, and snippets.

# 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|
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"
class Hash
def /(key)
self[key] || {}
end
end
h = {:foo => {:bar => {:baz => "BOOM"}}}
h/:foo/:bar/:baz # => "BOOM"
h/:foo/:bar/:not_existing # => nil
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
;; 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")
@fronx
fronx / fronx-4clojure-solution73.clj
Created September 16, 2011 17:49 — forked from anonymous/fronx-4clojure-solution73.clj
fronx's solution to Analyze a Tic-Tac-Toe Board ;; https://4clojure.com/problem/73
;; 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
;; 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 +))]
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.
class Mastermind
attr_reader :answer
def initialize(answer)
@answer = answer
end
def match(picks)
clues = []
picks.each_with_index do |pick, p_index|
;; fronx's solution to A Half-Truth
;; https://4clojure.com/problem/83
#(or (and (some false? %&) (some true? %&))
false)