Skip to content

Instantly share code, notes, and snippets.

View jbrains's full-sized avatar

J. B. Rainsberger jbrains

View GitHub Profile
@jbrains
jbrains / 1-Before.elm
Created August 1, 2017 22:51
How do I move the composition into the pipeline operating on Maybes?
changePlayerName : PlayerModel -> String -> ScorekeeperModel -> ScorekeeperModel
changePlayerName playerToChange newName scorekeeperModel =
let
{ players } =
scorekeeperModel
in
{ scorekeeperModel
| players =
Exts.List.mergeBy
(.id)
@jbrains
jbrains / 1-GraftNextCommitIfAny.sh
Last active July 23, 2017 17:56
Extract subproject from git repository by grafting commits onto the corresponding files
# CONTRACT
# SOURCE_SUBPRPOJECT_ROOT points to the "same" directory tree
# as TARGET_PROJECT_ROOT. We're extracting the directory tree
# at SOURCE_SUBPROJECT_ROOT into TARGET_PROJECT_ROOT.
#
# There must be a single, linear history in the SOURCE repository.
# Graft the current commit into the target
pushd "$SOURCE_SUBPROJECT_ROOT"
commit_comment="$(git log -1 --pretty=%B)"
@jbrains
jbrains / 1-Before.elm
Last active July 23, 2017 15:46
Removing duplication with Elm record syntax
playerDetailsViewModel : (PlayerId -> Int) -> PlayerModel -> PlayerDetailsViewModel
playerDetailsViewModel playerPointsScored playerModel =
PlayerDetailsViewModel
playerModel.id
playerModel.name
(playerPointsScored playerModel.id)
@jbrains
jbrains / 1-before.elm
Created July 2, 2017 16:22
Example: the value of describing a function with a comment
divFor : (a -> List (Attribute msg)) -> (a -> List (Html msg)) -> a -> Html msg
divFor cssStylesFor bodyFor blob =
-- Given functions that generate attributes and HTML elements for a thing,
-- wrap it in a div.
div
(cssStylesFor blob)
(bodyFor blob)
@jbrains
jbrains / 1-before-the-long-way.elm
Last active July 2, 2017 02:00
REVIEW: creating optional HTML from a Result in Elm
-- This becomes the typical pop-up error message at the top of an HTML page
failureView : Result String Int -> List (Html Update)
failureView possibleNumericInput =
case possibleNumericInput of
Err message ->
[ text message ]
Ok _ ->
[]
@jbrains
jbrains / drum-kit.html
Last active April 2, 2017 21:06
Finding functional/reusable bits in a small amount of Javascript. Especially trying to push details up the call stack.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS Drum Kit</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
@jbrains
jbrains / DispatchingEffectsFromOptionValue.java
Created December 3, 2016 18:05
PLEASE tell me that there's a better way in Java to write this
// findPrice : String -> Option<Price>
// Option comes from Atlassian's fugue library https://docs.atlassian.com/fugue/4.3.0/fugue/apidocs/index.html
// I want the equivalent of
// case (maybePrice) {
// none => display.displayProductNotFoundMessage(barcode)
// some(price) => display.displayPrice(price)
// }
catalog.findPrice(barcode)
.<Runnable>fold(
@jbrains
jbrains / PointOfSaleTerminal.java
Last active November 28, 2016 21:20 — forked from CoryFoy/sample.rb
TDD no Expectations
package ca.jbrains.pos;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.HashMap;
public class PointOfSaleTerminal {
public static void main(String[] args) throws IOException {
final InputStreamReader commandSource = new InputStreamReader(System.in);
@jbrains
jbrains / Before.java
Created November 27, 2016 21:02
How do I extract map(trim), then filter(!empty) into a method while keeping the pipeline style?
new StreamTextCommands()
.streamCommandsFrom(new InputStreamReader(System.in))
.map(String::trim)
.filter((command) -> !command.isEmpty())
.map(commandText -> sellOneItemController.onBarcode(commandText))
.map(CommandResponse::render)
.forEach(System.out::println);
@jbrains
jbrains / FindPricePossiblyReturningNull.java
Created November 27, 2016 20:11
I'm not convinced by this particular usage of Optional...
public CommandResponse onBarcode(String barcode) {
Price price = catalog.findPrice(barcode);
return price == null
? new ProductNotFoundMessage(barcode)
: new ProductFoundMessage(price);
}