Skip to content

Instantly share code, notes, and snippets.

View jbrains's full-sized avatar

J. B. Rainsberger jbrains

View GitHub Profile
@jbrains
jbrains / test.purs
Created December 24, 2021 14:11
WANTED: A more concise way to execute a side effect in a test without checking its result
checkSellMultipleItems :: Spec Unit
checkSellMultipleItems =
describe "Sell Multiple Items" do
it "happy path" do
(POS.handleCommand $ TextCommand "12345") `shouldSatisfy` (const true)
(POS.handleCommand $ TextCommand "total") `shouldEqual` "Total: CAD 27.50"
@jbrains
jbrains / VirtualPointOfSaleTerminal.java
Created April 30, 2019 13:32
A simple example of wiring an application together
package ca.jbrains.pos;
import ca.jbrains.pos.ui.TextProcessorAndCommandInterpreter;
import ca.jbrains.pos.ui.lcd.UdpPostOffice;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
public class VirtualPointOfSaleTerminal {
public static void main(String[] args) throws IOException {
@jbrains
jbrains / StringBuilderWithFolding.java
Created November 21, 2018 09:56
What obvious way of using Vavr's fold() am I missing here?
private String linesOf(List<String> lines) {
StringBuilder stringBuilder = new StringBuilder();
lines.map(line -> stringBuilder.append(line).append(System.lineSeparator()));
return stringBuilder.toString();
}
@jbrains
jbrains / DocumentConnectToDatabaseWithExposed.kt
Created November 4, 2018 20:22
First steps in using Kotlin, Exposed, and writing tests
import io.kotlintest.Description
import io.kotlintest.Spec
import io.kotlintest.extensions.TestListener
import io.kotlintest.shouldBe
import io.kotlintest.specs.FreeSpec
import org.jetbrains.exposed.dao.EntityID
import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.IntIdTable
import org.jetbrains.exposed.sql.Database
@jbrains
jbrains / LearnToMergeAFreemarkerTemplateTest.java
Created October 15, 2018 16:58
Why do we do this? Why isn't it easier just to create a template in memory and merge it with data in memory?
package ca.jbrains.org.freemarker;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import io.vavr.collection.HashMap;
import io.vavr.collection.Map;
import org.junit.Assert;
import org.junit.Test;
@jbrains
jbrains / LearnReadingLinesTest.java
Created October 13, 2018 19:16
An example of using a custom Hamcrest matcher to write tests. What do you think of this style?
package ca.jbrains.java.test;
import ca.jbrains.java.ReaderBasedTextSource;
import io.vavr.collection.List;
import io.vavr.collection.Stream;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Assert;
@jbrains
jbrains / factors.java
Created July 22, 2018 23:49
Find the number of factors of 5 in natural number n
public static int factorsOfFiveIn(int n) {
int factorsOfFive = 0;
while (n > 1) {
if (n % 5 == 0) factorsOfFive++;
n /= 5;
}
return factorsOfFive;
}
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
//Using Maybe.cs
namespace ArrayStructure
{
[TestFixture]
public class Tests
{
@jbrains
jbrains / ReturnConstructor.elm
Created December 6, 2017 10:35
Which, in your opinion, is "better"?
-- This version returns a constructor for a PlayerModel, needing only a "name"
playerNeedingName : Maybe PlayerModel -> List PlayerModel -> (String -> PlayerModel)
playerNeedingName selectedPlayer players =
selectedPlayer
|> Maybe.map withName
|> Maybe.withDefault (nextPlayerModel players)
@jbrains
jbrains / keybase.md
Created September 21, 2017 15:45
keybase.md

Keybase proof

I hereby claim:

  • I am jbrains on github.
  • I am jbrains (https://keybase.io/jbrains) on keybase.
  • I have a public key ASCk97WjVrw0fVggW5H9CdXC92koHvo0GSQrdW6T1q0j4Qo

To claim this, I am signing this object: