Skip to content

Instantly share code, notes, and snippets.

@kwijibo
kwijibo / chart.html
Created February 18, 2020 10:38
Estimated territorial greenhouse gas emissions by source category, UK 1990-2018
<body>
<div id="chart"></div>
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
<pre id="table3" style="display:none">
"Table 3: Estimated territorial greenhouse gas emissions by source category, UK 1990-2018"
Coverage: United Kingdom Million tonnes carbon dioxide equivalent (MtCO2e)
Sector More Detail 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
Energy supply 278.0 275.6 264.2 246.4 237.6 238.0 238.6 222.7 225.6 212.6 221.6 231.4 228.8 234.7 232.4 231.5 235.9 230.4 223.6 200.4 207.4 192.7 203.3 190.1 165.2 145.3 121.8 112.3 104.9
Power stations 204.2 200.8 188.7 171.2 167.0 163.9 163.6 150.9 156.1 147.9 159.4 170.3 165.9 175.1 174.7 174.0 182.9 178.7 173.6 151.8 158.1 145.4 159.4 148.4 125.2 105.0 83.0 73.1 66.8
Refineries 17.9 18.6 19.1 20.0 19.5 20.2 20.7 20.5 20.0 18.1 17.3 17.1 19.3 18.7 18.4 19.9 18.0 17.8 17.3 16.5
@kwijibo
kwijibo / climb_log.elm
Last active November 2, 2017 22:49
Beginner program for logging indoor climbing ascents
-- Read all about this program in the official Elm guide:
-- https://guide.elm-lang.org/architecture/user_input/forms.html
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput, onClick)
main =
Html.beginnerProgram
import React from 'react'
import ReactDOM from 'react-dom'
class App extends Component {
getInitialState(){
return {data: ''}
}
componentWillMount(){
this.props.dataPromise.then(data => this.setState({data}))
}
@kwijibo
kwijibo / reader.js
Last active February 17, 2017 13:10
staticland monads
const Reader = {
of: a => { return _ => a },
map: f => run => {
return x => f(run(x))
},
contramap: f => run => {
return x => run(f(x))
},
chain: f => run => {
return x => f(run(x))(x)
@kwijibo
kwijibo / task.js
Created October 7, 2016 14:44
Simple Task implementation
const Task = fork => ({
map: f => Task((reject, resolve)=>{ fork(reject, x => { resolve(f(x)) }) }),
chain: f => Task((reject, resolve)=>{ fork(reject, x => { f(x).fork(reject, resolve) }) }),
join: () => Task(fork).chain(i=>i),
ap: A => A.map(a => Task(fork).map(f => f(a))).join(),
fork
})
Task.of = x => Task((_,resolve)=>{ resolve(x)})
@kwijibo
kwijibo / patternmatching.js
Last active October 6, 2016 11:08
JS pattern matching
const Pattern = testers => transformers => val => {
const match = Object.keys(testers).find(k => testers[k](val))
return transformers[match](val)
}
const Num = Pattern({
Odd: n => n % 2,
Even: n => !(n % 2)
})
@kwijibo
kwijibo / hangman.js
Created April 19, 2016 08:51
Hangman
const gameIsWon = ({word, correctGuesses, wrongGuesses}) => wordIsGuessed(word, correctGuesses)
const gameIsLost = ({word, correctGuesses, wrongGuesses}) => listGTE10(wrongGuesses)
const wordIsGuessed = (word, guesses) => word.split('').every(c => guesses.includes(c))
const letterInWord = (letter, word) => word.split('').includes(letter)
const listGTE10 = (list) => Array.isArray(list) && list.length >= 10
function playHangman({word, correctGuesses, wrongGuesses}){
const letter = prompt('Guess a letter')
const goodGuess = letterInWord(letter, word)
const gameState = goodGuess? {word, correctGuesses: [letter, ...correctGuesses], wrongGuesses } : {word, correctGuesses, wrongGuesses: [letter, ...wrongGuesses]}
@kwijibo
kwijibo / search-trie.js
Created April 1, 2016 09:59
Search Trie
const branchLetter = ({full, sub},letter) => {
sub[letter] = sub[letter] || {}
return {full, sub: sub[letter] }
}
const indexWord = (trie, word) => {
const {full, sub} = word.split('').reduce(branchLetter, {full:trie, sub: trie})
return full
}
Diff = (before, changes) => { return {
map: f => {
const after = f(before)
const newChanges = diff(before, after)
return Diff(after, [...changes, ...newChanges])
}
}}