Skip to content

Instantly share code, notes, and snippets.

View JoeyEremondi's full-sized avatar

Joey Eremondi JoeyEremondi

View GitHub Profile
@JoeyEremondi
JoeyEremondi / 0_15_1.elm
Created August 21, 2015 03:01
Non-native Elm JS output: DCE vs no DCE
var Elm = Elm || { Native: {} };
Elm.Array = Elm.Array || {};
Elm.Array.make = function (_elm) {
"use strict";
_elm.Array = _elm.Array || {};
if (_elm.Array.values)
return _elm.Array.values;
var _op = {},
_N = Elm.Native,
_U = _N.Utils.make(_elm),
@JoeyEremondi
JoeyEremondi / BenchmarkPrototype.elm
Created July 16, 2015 00:12
Elm Benchmark API prototype
module Benchmark
( Benchmark
, Suite
, run
, bench1
, bench2
, bench3
, bench4
, bench5
, bench6
@JoeyEremondi
JoeyEremondi / elm-tco-internal.md
Last active December 8, 2016 22:43
Elm Tail Calls: Development Document

Elm TCO

The Transformation

For an example, let's look at the oneOf function from the Maybe module:

oneOf : List (Maybe a) -> Maybe a
oneOf maybes =
  case maybes of
@JoeyEremondi
JoeyEremondi / self-tail-call-opt.md
Last active August 29, 2015 14:24
Tail call Blog Post

A common misconception about functional languages is that they are slow, and that function calls is inherently slow compared to looping. However, most functional languages, and now Elm, provide a way for you to write loops: tail-recursion.

What is a Tail Call?

When you define a function, you will usually call other functions to build a return value. But sometimes, you directly return the value returned by another function. This is called a tail call.

Specifically, a an expression is in tail position if it is the body of the function, or if it is in tail position of the result of an if or case statement that is in tail position. Function calls made to calculate argument parameters, or used in the right-hand side of a let declaration, are not in tail position.

import Dict
import Random
import Graphics.Element
-- port initialSeed : Int
maxSize = 1000000
numToInsert = 10000
I'm going to talk about Monads as they are used in programming, without reference to the category theory (in no small part because I don't understand it entirely myself).
Monads are present in many languages, with various names. Elm has them as [Tasks](http://elm-lang.org/blog/announce/0.15.elm), and F# has them as [Computation Expressions](https://msdn.microsoft.com/en-us/library/dd233182.aspx). You could write something equivalent in JavaScript, Python, or any language supporting higher-order functions.
To understand what they are, you need to understand what a type constructor is. A type constructor is something which, when given a specific type as an argument, forms a type.
In Haskell notation, `List` is a type constructor. It's not a full type, because you don't know what it is a list of. `List Int` is a type, as is `List Bool`, or `List (List Double)`. This is similar to generics in Java or Templates in C++: you can't have just a List, you need `List<int>` or something like that.
Now, "monad" is an
As a learning experience, I'm trying to implement a verified regular-expression matcher using continuation-passing style in Agda, based on the one proposed in [this paper](http://www.cs.cmu.edu/~rwh/papers/regexp/jfp.pdf).
I've got a type for regular expressions defined like this:
<!-- language: lang-agda -->
data RE : Set where
ε : RE
∅ : RE
Lit : Char -> RE
@JoeyEremondi
JoeyEremondi / gist:b773c19c69d6ad2a4f07
Last active August 29, 2015 14:17
elm-record-poly-test
import Text (asText)
type alias Foo a b = {a | x:Int, y:b}
type alias Bar a b = {a | y:b, z:Int}
type alias FooBar a b = Foo (Bar a b) b
myRec : Foo (Bar {} Int) Int
@JoeyEremondi
JoeyEremondi / apiopts.hs
Last active August 29, 2015 14:15
Elm-public-API-options
import qualified AST.Module as Module
----------------------------------------------------------
-- These are the optimizing functions I'm writing that will be public
-- JS could be added to the output Map as well, if we wanted
{-| Given interfaces from compiled Elm modules and their compiled JavaScript,
attempt to optimize the given modules, assuming that the program will be run from
the entry point of `main` within the given main module-}
optimizeProgram
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(default ((t (:family "Source Code Pro" :foundry "adobe" :slant normal :weight semi-bold :height 113 :width normal)))))
;;Flycheck hook for haskell-mode
;;(eval-after-load 'flycheck
;; '(add-hook 'flycheck-mode-hook #'flycheck-haskell-setup))