Skip to content

Instantly share code, notes, and snippets.

View fmnoise's full-sized avatar
🇺🇦

fmnoise

🇺🇦
  • Yellowpay Inc
  • Norway
View GitHub Profile
@fmnoise
fmnoise / maybe.rb
Last active May 3, 2017 18:59
Maybe monad in Ruby
module Monads
class Maybe
def self.[](value)
self.new(value)
end
def initialize(value)
@value = value
end
@fmnoise
fmnoise / pipe.rb
Created May 3, 2017 19:34
Elixir-like pipeline in Ruby
class Pipe
def self.[] value
self.new value
end
def initialize value
@value = value
end
def >>
@fmnoise
fmnoise / monads.rb
Last active May 27, 2017 13:32
Simple Ruby implementations of some monads (just for fun)
module Monads
class Box
def self.[](value)
self.new(value)
end
def initialize(value)
@value = value
end
@fmnoise
fmnoise / extend-protocol-with-protocol.clj
Last active May 11, 2017 17:32
extending protocol with protocol
; inspired by example from "Clojure Applied"
; book says that it's not possible
(ns sales.core)
(defprotocol Cost
(cost [this]))
(defrecord Product [price quantity]
Cost
@fmnoise
fmnoise / moore.clj
Created May 27, 2017 12:33
Moore machine using atom
(defn make-transitions [& states]
(let [statemap
(zipmap
(map str states)
(rest (conj (vec states) (first states))))]
(fn [state] (statemap (str state)))))
(def moore-machine (atom :stopped))
(def transitions (make-transitions :starting :started :stopping :stopped))
@fmnoise
fmnoise / moore.clj
Created May 27, 2017 13:28
Moore machine macro
(ns machines.moore)
(defn make-state-fn [& states]
(let [statemap
(zipmap
(map str states)
(rest (conj (vec states) (first states))))]
(fn [state] (statemap (str state)))))
@fmnoise
fmnoise / 00_destructuring.md
Created June 17, 2017 22:03 — forked from john2x/00_destructuring.md
Clojure Destructuring Tutorial and Cheat Sheet

Clojure Destructuring Tutorial and Cheat Sheet

(Related blog post)

Simply put, destructuring in Clojure is a way extract values from a datastructure and bind them to symbols, without having to explicitly traverse the datstructure. It allows for elegant and concise Clojure code.

Vectors

@fmnoise
fmnoise / debug.clj
Created July 22, 2017 21:40 — forked from ato/debug.clj
Simpler debug-repl that works with unmodified Clojure
;; Inspired by George Jahad's version: http://georgejahad.com/clojure/debug-repl.html
(defmacro local-bindings
"Produces a map of the names of local bindings to their values."
[]
(let [symbols (map key @clojure.lang.Compiler/LOCAL_ENV)]
(zipmap (map (fn [sym] `(quote ~sym)) symbols) symbols)))
(declare *locals*)
(defn eval-with-locals
(require '[clojure.core.async :as a])
(def xform (comp (map inc)
(filter even?)
(dedupe)
(flatmap range)
(partition-all 3)
(partition-by #(< (apply + %) 7))
(flatmap flatten)
(random-sample 1.0)
@fmnoise
fmnoise / respond_to.clj
Created November 27, 2017 21:20
check if object can respond to method
(defn respond-to?
"Check if object can respond to method"
[obj method]
(boolean
(some->> obj
class
.getMethods
(filter #(= (.getName %) (name method)))
first
.getModifiers