Skip to content

Instantly share code, notes, and snippets.

@marcmartino
marcmartino / euler2.ts
Last active October 5, 2021 03:21
Euler 2
const fibUntil = (prevSum: number, prevNums = [0,1]): number => {
const nextNum = prevNums[0] + prevNums[1]
return nextNum >= 4_000_000
? prevSum
: fibUntil(
prevSum + (nextNum % 2 === 0 ? nextNum : 0),
[prevNums[1], nextNum]
)
}
@marcmartino
marcmartino / PatternMatching2.md
Created June 15, 2021 23:17
Less contrived pattern matching II

Less Contrived Pattern Matching Example II

Another example of a nice use of pattern matching I came upon while practicing basic hs- pattern match over a tuple of two booleans that you're creating ad hoc. This is great because it avoids the nesting of ifs/ternaries and is more clear what's going on.

getClassification :: Int -> Int -> Classification
getClassification target aliquotSum =
  case (target > aliquotSum, target < aliquotSum) of
    (True, _) -> Deficient
 (_, True) -&gt; Abundant
@marcmartino
marcmartino / Form.tsx
Last active June 3, 2022 09:08
Example io-ts react final form based off blitzjs boilerplate
import { TypeOf, Type, InputOf, OutputOf } from "io-ts"
import { matchW } from "fp-ts/Either"
import { reduce } from "fp-ts/Array"
import { flow } from "fp-ts/function"
import { ReactNode, PropsWithoutRef } from "react"
import { Form as FinalForm, FormProps as FinalFormProps } from "react-final-form"
export { FORM_ERROR } from "final-form"
export interface FormProps<C extends Type<TypeOf<C>, OutputOf<C>, InputOf<C>>>
extends Omit<PropsWithoutRef<JSX.IntrinsicElements["form"]>, "onSubmit"> {
@marcmartino
marcmartino / sublist.md
Created February 7, 2021 02:56
An Adventure in Functional Code Optimization

An Adventure in Optimizing Functional Code Speed

Continuing through my learning/exploration of elm I was tasked with coming up with a function for determining if a given list is an equal, sublist, superlist, or unrelated to another.

My initial solution looked like this

type ListComparison
    = Equal
    | Superlist
    | Sublist
    | Unequal
@marcmartino
marcmartino / removeLeadingZeros.md
Last active June 15, 2021 23:18
Less Contrived Pattern Match

Less Contrived Pattern Matching Example

Working my way through beginner elm exercises I needed to write a function to remove leading zeros from a list and it turned out to be a great illustration of how nice pattern matching can be.

in js the solution would be something like

const removeLeadingZeros = (xs) => 
          xs[0] === 0 
 ? removeLeadingZeros(xs.slice(1)) 
@marcmartino
marcmartino / range.md
Created February 7, 2021 00:27
Example of an imperative vs functional approaches

Imperative v Functional

Many times introduction to functional material tends to focus on really facile aspects of functional programming like "Use map or reduce instead of a for loop". To me this doesn't do justice to what a beginner functional approach really should be.

A good example to display how different the two approaches can be would be a simple range function

Imperative

function  range(start, end, step = 1) {
  let  numBetween = [];
 const rangeStep = start &lt; end ? Math.abs(step) : Math.abs(step) * -1;
@marcmartino
marcmartino / config.h
Last active February 7, 2022 19:03
dactyl keymap v2
/*
Copyright 2012 Jun Wako <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
@marcmartino
marcmartino / machine.js
Created August 24, 2020 20:15
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
let prod = false;
const getVizPromiseInput = () => {
return document.getElementById("serviceInput").value;
};
const clearVizPromiseInput = () => {
document.getElementById("serviceInput").value = "";
};
const disableViz = () => {
@marcmartino
marcmartino / machine.js
Last active June 8, 2020 01:56
Generated by XState Viz: https://xstate.js.org/viz
const setSpeechNodeContext = (lang, text) =>
assign({
speechText: text,
speechLanguage: lang,
});
const setListenNodeContext = (lang, conditions) =>
assign({
listenConditions: conditions,