Skip to content

Instantly share code, notes, and snippets.

{-| Keep equal elements that are at the same position in both lists
Examples:
>>> keepEqual "hello" "world"
"l"
>>> keepEqual (repeat 1) [0..10]
[1]
{-- | List the sub-sequences of conscutives elements in the list such that the
sum of the element is equal to the given number.
Examples:
>>> matchingSub 10 [1..5]
[[1,2,3,4]]
>>> matchingSub 2 $ replicate 3 1
[[1,1],[1,1]]
{- | given two positions in a list, swap them.
if (at least) one of the positions is invalid, return Nothing.
Examples:
>>> swapL 0 2 [0..5]
Just [2,1,0,3,4,5]
>>> fmap (take 6) $ swapL 0 2 [0..]
Just [2,1,0,3,4,5]
@rsslldnphy
rsslldnphy / to_donut.rb
Created May 13, 2014 16:35
My first submission to Ruby Core
class BasicObject
def to_donut
:donut
end
end
## Usage
3.to_donut #=> :donut
nil.to_donut #=> :donut
@rsslldnphy
rsslldnphy / xox_spec.rb
Created January 9, 2014 13:52
noughts and crosses
class Board
def squares
@squares ||= [[:_, :_, :_],[:_, :_, :_],[:_, :_, :_]]
end
def square(x, y)
squares[y][x]
end
def place(counter, x, y)
@rsslldnphy
rsslldnphy / request.clj
Created December 30, 2013 23:16
Functional HTTP request
(ns request)
(defn request [] {})
(defn body [] {})
(defn file [] {})
(defn set-url
[request url]
(assoc request :url url))
@rsslldnphy
rsslldnphy / Bob.pm
Created December 10, 2013 10:07
Bob in Perl5
use strict;
use warnings;
{
package Bob;
sub hey {
if (&_shouting) {
"Woah, chill out!"
} elsif (&_question) {
@rsslldnphy
rsslldnphy / dna.hs
Created November 15, 2013 17:41
Haskell RNA transcription!
-- Short version to show the power of Haskell :-)
module DNA(toRNA) where
toRNA = map convert where
convert 'T' = 'U'
convert n = n
-- Long version with a bunch of cool stuff that makes me happy
@rsslldnphy
rsslldnphy / model.rb
Created October 17, 2013 11:48
Looong methods! And code in the initializer!
module Id::Model
def initialize(data = {})
@data = data.reduce({}) do |acc, (k, v)|
field = fields[k.to_sym]
v ||= field.default!
v = field.type.new(v) if field.type.is_a?(Id::Model) && !v.nil?
acc.merge(k.to_s => Id::Hashifier.enhash(v))
end
end
@rsslldnphy
rsslldnphy / nutch_setup.sh
Created September 18, 2013 08:58
Sor and nutch setup
# set up java
sudo apt-get install openjdk-7-jdk
echo "export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64" >> ~/.bashrc
# get nutch 1.7
curl "http://mirror.ox.ac.uk/sites/rsync.apache.org/nutch/1.7/apache-nutch-1.7-bin.tar.gz" > nutch.tar.gz
tar zxvf nutch.tar.gz
cd ~/apache-nutch-1.7
# add some urls to crawl (optionally use seeds.txt in this gist)