Skip to content

Instantly share code, notes, and snippets.

@dminuoso

dminuoso/foo.rb Secret

Created November 10, 2017 22:53
Show Gist options
  • Save dminuoso/32666faa0dbfe7c0448bc34ded75b579 to your computer and use it in GitHub Desktop.
Save dminuoso/32666faa0dbfe7c0448bc34ded75b579 to your computer and use it in GitHub Desktop.
Identity = Struct.new(:value) do
def fmap(*args)
-> (func) {
Identity.new(func.call(self.value))
}.curry[*args]
end
end
Const = Struct.new(:value) do
def fmap(*args)
-> (func) { self }.curry[*args]
end
def self.make(*args)
-> (o) { new(o) }.curry[*args]
end
end
class Object
def deep_dup
dup
end
end
class Hash
def deep_dup
hash = dup
each_pair do |key, value|
if key.frozen? && ::String === key
hash[key] = value.deep_dup
else
hash.delete(key)
hash[key.deep_dup] = value.deep_dup
end
end
hash
end
end
class Proc
def *(other)
-> (*args) { self[other[*args]] }
end
end
class Array
def deep_dup
map(&:deep_dup)
end
def fmap(*args)
-> (fun, e) {
e.map(&fun)
}.curry[*args]
end
def self.nth(*args)
-> (n, o) {
o[n]
}.curry[*args]
end
def self.lens_at(*args)
-> (n) {
L.ens(nth(n), setter(n))
}.curry[*args]
end
def self.setter(*args)
-> (n, val, array) {
array.deep_dup.tap { |a| a[n] = val }
}.curry[*args]
end
end
module Kernel
def fmap(*args)
-> (fn, e) {
e.class.instance_method(:fmap).bind(e).call(fn)
}.curry[*args]
end
end
NoFunctor = Class.new(StandardError)
def Object
def fmap(*args, &blk)
raise NoFunctor if self.class == Object
if block_given?
self.class.instance_method(:fmap).bind(e).call(*args, &blk)
else
f, *restargs = args;
self.class.instance_method(:fmap).bind(e).call(f, *restargs)
end
end
end
def const(*args)
-> (val, _) { val }.curry[*args]
end
module L
class << self
def ens(*args)
-> (g, s, f, t) {
fmap(-> (f) {
s.(f, t)
}, w.(g.(t)))
}.curry[*args]
end
def over(*args)
-> (lens, fun, obj) {
lens.(-> (y) { Identity.new(fun.(y)) }).(obj).value
}.curry[*args]
end
def view(*args)
-> (lens, obj) {
lens.(Const.make).(obj).value
}.curry[*args]
end
def set(*args)
-> (lens, val, obj) {
over(lens, const(val), obj)
}.curry[*args]
end
end
end
class Hash
def self.path(*args)
-> (paths, hash) {
hash.dig(*paths)
}.curry[*args]
end
def self.setter(*args)
-> (paths, val, hash) {
hash.deep_dup.tap do |o|
*dir, final = paths
if dir.empty?
o.store(final, val)
else
o.dig(*dir).store(final, val)
end
end
}.curry[*args]
end
def self.lens_path(*args)
-> (paths) {
L.ens(path(paths), setter(paths))
}.curry[*args]
end
end
profile = {
username: 'dminuoso',
hobbies: ['fixing ruby', 'being weird'],
details: {
ability: 'writing functions',
},
friends: [
{
name: 'baweaver',
details: {
ability: 'puns',
}
}
],
enemies: [
{
name: 'Papierkorb',
details: {
ability: 'being mean :(',
}
}
]
}
flip = -> (a) { -> (f,s) { a[s,f] }.curry }
concat = -> (a, b) { a + b }
friend_l = Hash.lens_path([:friends])
ability_l = Hash.lens_path([:details, :ability])
hobbies_l = Hash.lens_path([:hobbies])
enemies_l = Hash.lens_path([:enemies])
first_l = Array.lens_at(0)
upcase = -> (e) { e.upcase }
scream = flip.(concat).('!!!') * upcase
puts L.over(
enemies_l * first_l * ability_l,
scream,
profile
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment