This file contains 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 User | |
attr_reader :hashed_pass | |
def initalize | |
@hashed_pass = "" | |
end | |
def change_profile_pic(other_pic) | |
@profile_pic = other_pic | |
end | |
end |
This file contains 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
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 |
This file contains 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
// 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); | |
}; |
This file contains 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
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 |
This file contains 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
;; Client | |
(import 'java.net.Socket) | |
(def output (.getOutputStream (Socket. "localhost" 3000))) | |
(.write output 3) |
This file contains 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
;; 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] |
This file contains 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 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] |
This file contains 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 state | |
case | |
when !collected? then 'booked' | |
when !ironed? then 'collected' | |
when !delivered? then 'ironed' | |
else 'delivered' | |
end | |
end |
This file contains 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
(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 |
This file contains 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
(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 |