Last active
November 8, 2017 19:54
-
-
Save hankyates/c045ab27e626fa80a19bef9cbeff8576 to your computer and use it in GitHub Desktop.
code-share
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Just screwing around | |
function exclaim(message = '') { | |
return message + '!'; | |
} | |
function append(src = '', tgt = ''){ | |
return src + tgt; | |
} | |
function namedExclaim(message = '') { | |
return append(message, '!'); | |
} | |
var lambdaExclaim = m => append(m, '!'); | |
var partialExclaim = R.partialRight(append, '!'); | |
function messageMaker(singleMsg= {}) { | |
var {name, message, date} = singleMsg; | |
var nameTag = append(name, ': '); | |
var dateTag = parens(date); | |
return append( | |
append(nameTag, message), | |
append(' ', dateTag) | |
); | |
} | |
function wrap(left, body, right) { | |
return append(append(left, body), right); | |
} | |
var parens = (b) => wrap('(', b, ')'); | |
var messages = [ | |
{ | |
name: 'Hank', | |
message: 'Hey everyone', | |
date: moment('2014-09-08T08:02:17-05:00').format("ddd, hA") | |
}, | |
{ | |
name: 'Shawn', | |
message: 'How are you?', | |
date: moment('2014-09-08T10:02:17-05:00').format("ddd, hA") | |
}, | |
{ | |
name: 'Hank', | |
message: 'Im great.', | |
date: moment('2014-09-08T16:02:17-05:00').format("ddd, hA") | |
} | |
]; | |
console.log( | |
messages.map(messageMaker).join('\n') | |
); | |
var min = (p, c) => p > c ? c : p; | |
var max = (p, c) => p > c ? p : c; | |
console.log( | |
[1, 2, -15, 12].reduce(max) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Trying to create a logging function that can be used to `tap` function compositions. | |
let createLog = logFn => pipe( | |
over([ | |
identity, | |
logFn | |
]), | |
take(1) | |
) | |
let log = createLog(console.log) | |
let parseNames = pipe( | |
log, | |
split(','), | |
log, | |
map(trim) | |
) | |
const NAMES = 'bill, jane, fred' | |
log(parseNames(NAMES)) | |
// -> ['bill', 'jane', 'fred'] | |
// logs: 'bill, jane, fred' | |
// logs: ['bill', ' jane', ' fred'] | |
// logs: ['bill', 'jane', 'fred'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Just wanted to post this snippet of how to initialize an Elm app with an http request | |
-- Main.elm | |
init : (Model, Cmd Msg) | |
init = | |
{- | |
Task.perform returns a `Cmd Msg` so basically you call the Http.get | |
from your other module. Then wire your messages up here. | |
-} | |
(Model True Gists.init Nothing, Task.perform Fail GistsSuccess Gists.getGist) | |
type Msg | |
= Loading Bool | |
-- The success Msg needs to match what you are expecting the decoder to return. | |
| GistsSuccess (List Gists.Model) | |
| Fail Http.Error | |
-- Gists.elm | |
getGist : Task.Task Http.Error (List Model) | |
getGist = | |
let | |
url = | |
"https://api.github.com/gists/[gistId]" | |
in | |
Http.get decodeGists url | |
decodeGists : Decoder (List Model) | |
decodeGists = | |
{- | |
This took a while to figure out. The `files` key was on an object where the | |
key was the file name. I just wanted to return a List instead of an Object/Dict. | |
`keyValuePairs` returns a List then the lambda's below just discard the filename | |
string and return the model. Notice there are two maps here. The first is a | |
Decoder.map mean for use with maping through Decoder structures. We want to return | |
a real List so we use List.map to create our `(List Model)` | |
-} | |
"files" := fileDecoder |> map (\t -> List.map (\(filename, model) -> model) t) | |
fileDecoder : Decoder (List (String, Model)) | |
fileDecoder = | |
gistDecoder | |
|> keyValuePairs | |
gistDecoder : Decoder Model | |
gistDecoder = | |
object2 Model | |
("filename" := string) | |
("content" := string) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+-----------------------------------------------------------------------------------+ | |
| href | | |
+----------+--+-----------+--------------------+-----------------------------+------+ | |
| protocol |* | auth | host | path | hash | | |
| | | +-------------+------+----------+------------------+ | | |
| | | | hostname | port | pathname | search | | | |
| | | | | | +-+----------------+ | | |
| | | | | | | | query | | | |
" http: // user:pass@some.host.com:65535 /path/name ? search=1&continues#hash " | |
| | | | | | | | | | | |
+----------+--+-----------+-------------+------+----------+-+----------------+------+ | |
(all spaces in the "" line should be ignored -- they're purely for formatting) | |
*: given by "slashes" key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I wanted to try and implement a declarative | |
// interface that could replace javascripts | |
// switch case statement. I screwed around | |
// for a and came up with this. | |
var when = variable => { | |
// The inherent problem is that in order to | |
// chain function calls together. We need to | |
// store some state. Which means somewhere | |
// there is going to be a side effect. `result` | |
// is the side effect. | |
var result; | |
var updateWith = f => { | |
result = f | |
return chain | |
} | |
var chain = { | |
is: (is, value) => (variable === is) ? updateWith(value) : chain, | |
// I wanted there to always be a default case. So I decided | |
// otherwise should be the terminator that returns the value | |
// or the default. | |
otherwise: (value) => result ? result : value | |
} | |
return chain | |
} | |
when('wat') | |
.is('wat', {wat: 'wat'}) | |
.otherwise({}) | |
// -> {wat: 'wat'} | |
when('not wat') | |
.is('wat', {wat: 'wat'}) | |
.otherwise({}) | |
// -> {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment