Skip to content

Instantly share code, notes, and snippets.

View dsp's full-sized avatar
💭
it's complicated

David Soria Parra dsp

💭
it's complicated
View GitHub Profile
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"),
void drop(List a, int n) {
...
}
List l = ...
l.drop(1); // this translates to drop(l, 1);
struct List {
List* next;
void add(...) {
}
}
T[] filter(alias fun, T)(T[] tofilter) {
auto result = [];
foreach (auto e; tofilter) {
if (fun(e)) {
result ~= e;
}
}
return result;
}
import std.stdio;
int main(string[] args) {
auto arr = ["apple", "bananas", "cherries"];
foreach (auto e; arr) {
writefln("got %s", e);
}
return 0;
}
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"
(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*))
(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)))))
(defn filter-keys [coll & keys]
(apply hash-map
(mapcat identity
(filter #(contains? (apply hash-set keys) (key %)) coll))))
(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