Skip to content

Instantly share code, notes, and snippets.

View defer.js
var waitall = {
defer: [],
final: null
}
// will make sure to call `waitall.final` when all `waitall.defer` has cleared
function defer (fn) {
waitall.defer.push(fn)
return function (...args) {
var result = fn(...args)
@choonkeat
choonkeat / diy.cloudmailin.bash
Created Aug 1, 2012
receiving (postfix) emails via (rails) http; using file upload (-F) for less verbose Rails log (otherwise use --data-urlencode)
View diy.cloudmailin.bash
#!/bin/bash
MAILFILE=/tmp/mail.$$
CURLFILE=/tmp/mail.$$.curl
cat > $MAILFILE
curl -i -F message=@$MAILFILE "http://localhost/incoming_messages" > $CURLFILE
if grep 'HTTP/1.1 204 No Content' $CURLFILE
then
rm -f $CURLFILE $MAILFILE
fi
@choonkeat
choonkeat / App.elm
Last active Oct 10, 2020
various Main.elm templates (app, minimum, bootstrap)
View App.elm
module App exposing (Flags, Model, Msg(..), init, main, subscriptions, update, view)
import Browser
import Browser.Navigation
import Html exposing (Html, text)
import Url
main =
Browser.application
View elm-is-not-a-frontend-framework.md
  • Elm is not a frontend framework.
  • Elm is neither V in MVC, nor MV in MVC, nor even MVC itself.
  • You don't assemble libraries and configure options to make Elm do what you want.

Elm is a language. You write programs with it.

But instead of providing a regular main function to run, Elm wants you to write at least 2 parts to run your program: init and update. This is my pseudo code for your init and update plugs into Elm runtime:

let [globalState, cmd] = init(optionFlags)
View what-does-it-mean-by-html-msg.md

What does it mean by Html Msg?

There are possibly 2 questions in this question. I'll answer them 1 by 1

Part 1: h-t-m-l message

After knowing the basic Elm syntax, we could be reading the following "return values" like this

userFromJWT : String -> Maybe User
View functions-for-increased-reuse.md

Scenario: Given a cookie string (which may be absent), parse and return a User record/struct

Often we'd write functions where arguments are "nullable", aka *string in Go, aka a Maybe in Elm

-- function `userFromCookie` with parameter type `Maybe String` return type `User`
userFromCookie1 : Maybe String -> User
userFromCookie1 maybeString =
    case maybeString of
        Nothing ->
@choonkeat
choonkeat / AWS.elm
Last active Aug 1, 2020
"Signing AWS requests with Signature Version 4" in Elm https://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html
View AWS.elm
module AWS exposing (Config, HttpRequest, Service(..), httpTask, sign__)
import Base16
import Crypto.HMAC
import Crypto.Hash
import DateFormat
import Http
import Json.Encode
import Task exposing (Task)
import Time
View FormData.elm
{-| Maybe like how data from remote server can be modelled with 4 states `krisajenkins/remotedata`
perhaps form input can be modelled similarly too, giving us a convenient way to work with them
`err` stores validation errors for the form values, e.g. Dict String String
`a` stores the raw value from user input, e.g. Dict String String
`b` is the data type that we expect to wield if `a` is valid, e.g. API.CreateUser.Input
-}
type FormData err a b
= NotValid a err
View gist:9815130
condition = false
puts "hello" if condition
#=> nil
# note there is no printing of `hello`, this means `conditions` was evaluated first before performing the preceding code
# this is expected and expanding the preceding code into `begin...end` yields you the same result
condition = false
begin
puts "hello"
end if condition
@choonkeat
choonkeat / CmdWorkerPool.elm
Last active May 31, 2020
Module that allows Elm app to execute N `Cmd` in parallel (queue the remaining `Cmd`), when one is done execute the next Cmd in queue
View CmdWorkerPool.elm
module CmdWorkerPool exposing (Request, State, init, request, update)
import Task exposing (Task)
{-| State
- workersLimit limits the maximum number of concurrent Cmd
- workersCount tracks the current number of Cmd in progress
- backlog stores a List of Cmd that are waiting to be dispatched