Skip to content

Instantly share code, notes, and snippets.

@jamesmacaulay
jamesmacaulay / AnyDecoderBroken.elm
Last active March 29, 2016 15:39
AnyDecoder{Broken,Fixed}.elm
-- This module doesn't work on arbitrarily nested JSON.
-- See the AnyDecoderFixed module, also in this gist,
-- for a version that does work with any JSON.
module AnyDecoderBroken where
import Json.Decode as Js exposing (Decoder, (:=))
import Dict exposing (Dict)
import Array exposing (Array)
import Graphics.Element exposing (show)
-- inspired by: https://groups.google.com/d/msg/elm-discuss/2LxEUVe0UBo/ZgJ_ldUH6ygJ
-- thanks for the help: http://learnyouahaskell.com/functors-applicative-functors-and-monoids
module UserDecoder where
import Date exposing (Date)
import User exposing (User)
import Json.Decode as Js exposing ((:=))
-- Applicative's `pure`:
module AttributeReaders
class <<self
# str = "foo"
# overrides = {
# another_reverse: :reverse,
# exclaimed: ->(str) { str + "!" },
# to_sym: ->(str) { str.reverse.to_sym }
# }
# reader = AttributeReaders.object_reader(str, overrides)
# [:reverse, :another_reverse, :exclaimed, :to_sym].map(&reader)
import Mouse
import Array
buildAppDataMap delta pos history =
{ frameDelta=delta
, mousePosition=pos
, clickHistory=history }
arrayTakeLast n array =
let inputLen = Array.length array
debounce : Time -> Signal a -> Signal a
debounce t sig = sig |> sampleOn (Time.since t sig |> keepIf not False)
@jamesmacaulay
jamesmacaulay / gist:375a1fc9eacded79bcdc
Created June 14, 2014 16:58
github.io https -> http redirect
$ curl -I https://shopify.github.io/dashing/
HTTP/1.1 200 OK
Date: Sat, 14 Jun 2014 16:57:15 GMT
Server: GitHub.com
Content-Type: text/html; charset=utf-8
Last-Modified: Wed, 07 May 2014 03:44:03 GMT
Expires: Sat, 14 Jun 2014 17:07:15 GMT
Cache-Control: max-age=600
Content-Length: 15154
Accept-Ranges: bytes
class Module
# Takes symbols representing instance methods defined in the module.
# Returns a new anonymous module with only those instance methods defined.
def slice(*methods)
src = self
Module.new do
methods.each do |sym|
define_method(sym, src.instance_method(sym))
end
end
@jamesmacaulay
jamesmacaulay / debounce.clj
Last active January 25, 2016 09:29
debounce with core.async
(defn debounce
([input ms] (debounce (chan) input ms))
([output input ms]
(go
(loop [val (<! input)]
(let [t (timeout ms)
[next-val port] (alts! [input t])]
(if (= port t)
(do
(>! output val)
@jamesmacaulay
jamesmacaulay / gist:8264393
Created January 5, 2014 04:38
js notes 2014-01-04

wtfjs

wtf parens scope?

(function isBound() {return !!isBound}); isBound();
// ReferenceError: isBound is not defined

function isBound() {return !!isBound}; isBound();
// true
(ns jamesmacaulay.console)
(defn log [& xs] (.apply console/log js/console (apply array xs)))
(defn logjs [& xs] (.apply console/log js/console (clj->js xs)))