Skip to content

Instantly share code, notes, and snippets.

View domgetter's full-sized avatar

Dominic Muller domgetter

View GitHub Profile
class User
attr_reader :hashed_pass
def initalize
@hashed_pass = ""
end
def change_profile_pic(other_pic)
@profile_pic = other_pic
end
end
db = [{name: "Jim", secret: "I peed my pants.", super_secret: "I love Nickleback."}]
user = {name: "Jim"}
get_secret = -> user { secret = db.find {|db_user| db_user[:name] == user[:name]}[:secret]; user.merge({secret: secret}) }
get_super_secret = -> user { super_secret = db.find {|db_user| db_user[:name] == user[:name]}[:super_secret]; user.merge({super_secret: super_secret}) }
bind = -> x, f { x[:secure] ? f[x[:value]].merge({secure: true}) : x } # security-aware manner of calling functions/methods
unit = -> n { {secure: false, value: n} } # makes objects security-aware
lift = -> f { -> x { unit[f[x]] } } # makes functions/methods security-aware
// Run online: https://eval.in/506636
#include <iostream>
#include <functional>
// curries any function from (int, int) -> int into int -> (int -> int)
auto curry(int (*func)(int, int)) -> std::function<std::function<int (int)> (int)> {
return [func] (int x) {
return [func, x] (int y) {
return func(x, y);
};
irb(main):010:0> Parser::CurrentRuby.parse("\"123\" =~ /1/")
=> (send
(str "123") :=~
(regexp
(str "1")
(regopt)))
irb(main):011:0> Parser::CurrentRuby.parse("\"123\" !=~ /1/")
=> (send
(str "123") :!=
(send
;; Client
(import 'java.net.Socket)
(def output (.getOutputStream (Socket. "localhost" 3000)))
(.write output 3)
;; Use defroutes to create a defresource macro that generates restful routes like
;; Rails: http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
(def restful-routes
[['GET "" "/index" ]
['GET "/new" "/new" ]
['POST "" "/create" ]
['GET "/:id" "/show" 'id]
['GET "/:id/edit" "/edit" 'id]
['PATCH "/:id" "/update" 'id]
class Object
def repeatedly
Enumerator.new do |y|
loop { y << yield(self) }
end
end
end
[3,4,5].repeatedly(&:sample).take(10)
#=> [4, 3, 4, 4, 4, 4, 3, 5, 5, 5]
def state
case
when !collected? then 'booked'
when !ironed? then 'collected'
when !delivered? then 'ironed'
else 'delivered'
end
end
(defn find-with-key
([ds key] (find-with-key (:root ds) key [:root]))
([ds key path]
(if (= key (:key (:data ds)))
path
(if (empty? (:children ds)) nil
(first (filter identity (for [[index child] (map-indexed list (:children ds))]
(find-with-key ((:children ds) index) key (conj path :children index)))))))))
(def my-map {:key nil
(defn partition-while
"partitions elements in order while the predicate function holds
the predicate function is given the current element
and the current partition. "
[f coll]
(loop [truck [] box [] [book & books] coll]
(if (empty? books) ; if there are no more books to pack
(conj truck box) ; tape up the last box, put it in the truck, and return the truck
(if (f book box) ; otherwise, if we can put the current book in the pending box
(recur truck (conj box book) books) ; put the book in the box and work on the next book