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
pub enum Owned<T> { | |
Empty, | |
Mine(T), | |
} | |
impl<T> Owned<T> { | |
pub fn take(&mut self) -> T { | |
match std::mem::replace(self, Owned::Empty) { | |
Owned::Mine(t) => t, | |
Owned::Empty => panic!("Can't call take on Empty"), |
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
void drop(List a, int n) { | |
... | |
} | |
List l = ... | |
l.drop(1); // this translates to drop(l, 1); |
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
struct List { | |
List* next; | |
void add(...) { | |
} | |
} |
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
T[] filter(alias fun, T)(T[] tofilter) { | |
auto result = []; | |
foreach (auto e; tofilter) { | |
if (fun(e)) { | |
result ~= e; | |
} | |
} | |
return result; | |
} |
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
import std.stdio; | |
int main(string[] args) { | |
auto arr = ["apple", "bananas", "cherries"]; | |
foreach (auto e; arr) { | |
writefln("got %s", e); | |
} | |
return 0; | |
} |
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
import System.IO | |
import Control.Concurrent | |
seconds ms = ms * 1000000 | |
repfn fn t = do threadDelay t | |
fn | |
repfn fn t | |
main = repfn fn $ seconds 1 | |
where fn = do putStr "hello world\n" |
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 base32enc | |
"Returns a base32 encoded strings of the byte vector | |
A base32 character can represent 5 bits. We split the vector | |
into chunks of 5 bytes = 40 bits = 8 * 5 bits and create a 40 bit | |
Long representation of the 5 bytes. We then use Long/toString to | |
get the 8 characters base32 representation. | |
" | |
([vec] | |
(base32enc vec *base32-alpha*)) |
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
(ns | |
#^{:doc "Utilities"} | |
gh.util | |
(:import java.security.MessageDigest)) | |
(defn digest | |
"Calculates a digest with the given algorithm" | |
([str] (digest str "SHA1")) | |
([str dig] | |
(apply vector (.digest (MessageDigest/getInstance dig) (.getBytes str))))) |
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 filter-keys [coll & keys] | |
(apply hash-map | |
(mapcat identity | |
(filter #(contains? (apply hash-set keys) (key %)) coll)))) |
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 q | |
([a x] | |
(* 1/2 (+ x (/ a x))))) | |
(defn lazy-q | |
([a] (concat [a] (lazy-q a a))) | |
([a x] | |
(let [r (q a x)] | |
(lazy-seq | |
(cons r |
NewerOlder