Skip to content

Instantly share code, notes, and snippets.

View zoren's full-sized avatar

Søren Sjørup zoren

  • Copenhagen, Denmark
View GitHub Profile
let rec f _ =
let _ = myignore 1
myignore () // not ok, myignore expects int as argument type
and myignore _ = ()
let rec myignore _ = ()
and f _ =
let _ = myignore 1
myignore () // ok, myignore accepts any type as argument type
@zoren
zoren / gist:5116859
Last active December 14, 2015 16:39
private static Array DownCastArray<TE>( TE[] ar, Type elementType ) {
Array castDown = Array.CreateInstance( elementType, ar.Length );
for ( int i = 0; i < ar.Length; i++ ) {
object value = ar[i];
if ( !elementType.IsInstanceOfType( value ) ) {
throw new ArgumentException( "df" );
}
castDown.SetValue( value, i );
}
return castDown;
@zoren
zoren / operator precedence parser.swift
Created April 19, 2016 13:15
A implementation of the Operator precedence parser from Wikipedia in Swift 2
// Created by Søren Sjørup on 16/04/16.
// Copyright © 2016 Søren Sjørup. All rights reserved.
import Foundation
indirect enum Exp{
case C(Int)
case Bin(Exp, String, Exp)
}
### Keybase proof
I hereby claim:
* I am zoren on github.
* I am zoren (https://keybase.io/zoren) on keybase.
* I have a public key whose fingerprint is 6CB1 901B E1F1 877D E4C6 353A 55FD 41CE 594E 9B0C
To claim this, I am signing this object:
@zoren
zoren / hackerrankprelude.fsx
Created December 9, 2016 13:11
My hacker rank prelude for F#
open System
open System.Collections.Generic
let rl = Console.ReadLine
let splsp (s:string) = s.Split([|' '|], StringSplitOptions.RemoveEmptyEntries)
let getTwo (a:_[]) = a.[0], a.[1]
let getThree (a:_[]) = a.[0], a.[1], a.[2]
let callN n action = seq { for i = 1 to n do yield action()} |> Seq.toArray
let iterrl f =
let rec loop () =
let l = rl()
@zoren
zoren / gist:4eb9543395d743670154b7b10059e608
Last active June 19, 2020 09:09 — forked from biggert/gist:6453648
Get a clojure reader using the encoding by the BOM
(ns util
(:require [clojure.java.io :as io])
(:import org.apache.commons.io.input.BOMInputStream
org.apache.commons.io.ByteOrderMark))
(defn bom-reader
"Returns a BOM contextual reader with the proper encoding set (= BOM), defaults to UTF-8"
[input-stream]
(let [bom-array
(into-array [ByteOrderMark/UTF_16LE
@zoren
zoren / kotlin.kt
Last active November 9, 2020 12:04
interface I<TP> {
fun TP.func()
}
class Dummy
class Implementer : I<Dummy> {
override fun Dummy.func() {
println(this)
}
(defn index-of-safe
"Return index of value (string or char) in s, optionally searching
forward from from-index. Return nil if value not found.
Works around https://bugs.openjdk.java.net/browse/JDK-8027640
so the returned index is always greater equal to from-index.
This also works around JavaScript's similar behavior."
[s value from-index]
(if (< (count s) from-index)
nil
(clojure.string/index-of s value from-index)))
@zoren
zoren / download-blob.cljs
Created June 18, 2020 15:21
Download a BLOB via a object URL in ClojureScript
;; heavily inspired by Mariano Guerra
;; http://marianoguerra.org/posts/download-frontend-generated-data-to-a-file-with-clojurescript.html
(defn download-blob [file-name blob]
(let [object-url (js/URL.createObjectURL blob)
anchor-element
(doto (js/document.createElement "a")
(-> .-href (set! object-url))
(-> .-download (set! file-name)))]
(.appendChild (.-body js/document) anchor-element)
(.click anchor-element)
@zoren
zoren / surprises.clj
Last active February 23, 2022 16:14
surprises in Clojure land
;; some surprises found while learning Clojure
;; min is for numbers
(min #inst "2018" #inst "2019") ; Execution error (ClassCastException) at clojure-test.core/eval8469 (form-init524028722621051551.clj:2589) .
; java.util.Date cannot be cast to java.lang.Number
;; macros are not values
(apply or (list false true)) ; Syntax error compiling at (Untitled-1:2:1).
; Can't take value of a macro: #'clojure.core/or