Skip to content

Instantly share code, notes, and snippets.

@ssirowy
ssirowy / wordle-fp-ts.ts
Last active March 20, 2022 23:07
Wordle in FP-TS
import { ap, of, intersection } from 'fp-ts/Array'
import { Eq as StringEq } from 'fp-ts/string'
import { pipe } from 'fp-ts/function'
// Our word bank, shortened for brevity.
const wordBank = ['wince', 'hinge', 'movie', 'arbor' /** More */]
// Here is the function I need to write that can evaluate individual solutions. Note I have
// to write it in a curried form.
const word = (a: string) => (b: string) => (c: string) => `${a}i${b}${c}e`
const allWords = R.lift((a, b, c) => `${a}i${b}${c}e`)
const bank = ['aahed','aalii','aargh','aarti','abaca','abaci','aback','abacs','abaft','abaka','abamp','aband','abase','abash','abask','abate','abaya','abbas','abbed','abbes','abbey','abbot','abcee','abeam','abear','abele','abers','abets','abhor','abide','abies','abled','abler','ables','ablet','ablow','abmho','abode','abohm','aboil','aboma','aboon','abord','abore','abort','about','above','abram','abray','abrim','abrin','abris','absey','absit','abuna','abune','abuse','abuts','abuzz','abyes','abysm','abyss','acais','acari','accas','accoy','acerb','acers','aceta','achar','ached','aches','achoo','acids','acidy','acing','acini','ackee','acker','acmes','acmic','acned','acnes','acock','acold','acorn','acred','acres','acrid','acros','acted','actin','acton','actor','acute','acyls','adage','adapt','adaws','adays','adbot','addax','added','adder','addio','addle','adeem','adept','adhan','adieu','adios','adits','adman','admen','admin','admit','admix','adobe','adobo','ad
@ssirowy
ssirowy / declarative-programming-by-example.md
Last active December 15, 2021 01:04
A declarative programming exercise

Declarative programming by example

Chris approached me on Friday and indicated he wrote a small utility for dealing with shortcuts and wondered if it made sense to use functional programming techniques to improve it.

Without seeing the code, I clarified that functional programming is nothing more than primarily using functions (with a few other techniques) to generate your desired value/data structure.

What I think Chris meant was he wanted to use some more declarative functional techniques to help specify what the program should do, and not necessarily how it should be done (e.g put this result into this variable, use a set of control structures to decide what to do next, etc). It's a very different model of programming, and while unfamiliar to some, can make programs more readable, more concise, and less prone to bugs.

The goal of is article is to lead the reader from a small imperative implementation to more a declarative one through a series of small, focused derivative changes.

T

/*
Here is our code that creates their Unit test, and then calls their `passed`. Its in here
we need a mechanism for providing their function a way to record a string that we can then record in our DB.
*/
public class zyMainUnitTestRunner {
public static void main(String [] args) {
zyLabsUnitTest test = null;
try {
test = new zyLabsUnitTest();
/**
* Instructors in our system have the ability to write a *unit test* against their students code
* using the following interface. The *only* thing they can edit is the contents of the `passed` function.
* But, they can create a student's object, run their own code, and pass us a true/false value as to whether
* the test passed.
*/
public class zyLabsUnitTest {
public boolean passed() {
// Write your code here.
// e.g return student_add(4, 2) == 6;
@ssirowy
ssirowy / gist:8359884
Created January 10, 2014 18:32
ROTW: 1/10/2014
Part A) Farewell
At a train station, the Porter family is saying goodbye to the Robinson family.
We don't know who is leaving and who is staying.
Each of the members of the Porter family say farewell to each of the members of the
Robinson family. To say goodbye, two men shake hands, and both a man and woman and two
women kiss once on the cheek.
An eyewitness to event counted 21 handshakes and 34 kisses. How many men and how many
@ssirowy
ssirowy / gist:7680852
Created November 27, 2013 18:35
ROTW 11/27/2013
Part A)
Grandma said she'd make her homemade cherry pie if you can figure out this problem:
Given 3 bowls and 12 cherries, can you arrange the fruit so that there an odd number of
cherries in each bowl? Oh, and each bowl must contain more than 3 cherries.
Part B)
Find a four digit number, with four different digits, that is equal to number formed
by its digits in descending order minus the number formed by its digits in ascending
order
@ssirowy
ssirowy / gist:7489279
Created November 15, 2013 18:30
ROTW: 10/15/2013
You must answer both questions to win this week:
A) Two boys walking in the woods decided to take a shortcut through a railroad tunnel.
When they had walked 2/3 of the way through the tunnel, their worst fears were
realized. A train was coming in the opposite direction, nearing the tunnel entrance.
The boys panicked and each ran for a different end of the tunnel. Both boys ran
at the same speed, 10 miles per hour. Each boy escaped from the tunnel just at the instant
that the train would have squashed him into the rails. Assuming the train's speed was
constant, and both boys were capable of instantaneous reaction, and acceleration,
how fast was the train going?
@ssirowy
ssirowy / gist:7374321
Last active December 27, 2015 18:59
ROTW: 11/8/2013
You must answer both to win ROTW this week
A) The answers to the questions below are one-digit numbers from one to nine,
and no number is repeated. Figure out the unique set of answers:
1. Answer to statement 5 minus answer to statement 4.
2. Answer to statement 8 times answer to statment 9.
3. Answer to statement 4 plus answer to statement 8.
4. Number of times that the number 9 is in an answer.
5. Total number of statements.
@ssirowy
ssirowy / gist:7269318
Created November 1, 2013 18:04
ROTW: 11/1
There are two parts to this week's riddle of the week. You must answer both to win this week's ROTW
A) In an athletics contest between the army, the navy, and the air force, each team entered
three athletes in a particular race. The winning athlete scored 8 points, the runner-up 7,
third place 6, and so on down to none for last place.
Once the race was won, the judges needed a photograph to separate the first two army men to finish.
A member of the navy's team finished last. When the points were totaled, all three teams were
found to have the same score.