Skip to content

Instantly share code, notes, and snippets.

View Norwae's full-sized avatar

Norwae

View GitHub Profile
module Attaxx (Coordinate, Move(..), CellState(..), Board, get, initial, moves, movesFor, applyMove, indices, neighbours) where
import Data.Array hiding (indices)
import Data.Maybe
import Data.Char
import Prelude
data Coordinate = Coordinate { x:: Int, y :: Int} deriving (Show, Eq, Ord, Ix)
data ShortDirection = ShortNW | ShortN | ShortNE | ShortW | ShortE | ShortSW | ShortS | ShortSE deriving (Enum, Show)
data LongDirection = LongNW | LongNNW | LongN | LongNNE | LongNE | LongWNW | LongENE | LongW | LongE | LongWSW | LongESE | LongSW | LongSSW | LongS | LongSSE | LongSE deriving (Enum, Show)
The origin of the question is http://stackoverflow.com/q/25949977/3185992 where user Sri asked about a problem he had.
The problem is not that hard to solve, but the answers got me concerned that there must be a more elegant way of doing this.
I wanted something with meaningful intermediate results, which is composable and not some nested for loops which at the end will result in the right thing.
Problem description:
====================
Given two strings compute the count of characters which are not common in both of them.
However each character "counts of its own", that is if a character is included in the first string 2 times and a single time in the second one, the right answer for this character is: 1.
Example from the user:
@Norwae
Norwae / Roman.hs
Last active August 29, 2015 14:21
Roman numerals (how I wanted it to work...)
data PseudoDigit = PseudoDigit {
value :: Int,
display :: String
} deriving Show
digits = [ PseudoDigit 1000 "M", PseudoDigit 900 "CM", PseudoDigit 500 "D", PseudoDigit 400 "CD", PseudoDigit 100 "C", PseudoDigit 90 "XC",
PseudoDigit 50 "L", PseudoDigit 40 "XL", PseudoDigit 10 "X", PseudoDigit 9 "IX", PseudoDigit 5 "V", PseudoDigit 4 "IV", PseudoDigit 1 "I"]
highestDigit x = head $ filter (\p -> x >= value p) digits
@Norwae
Norwae / Moo.scala
Last active August 29, 2015 14:25
Reusable bit of js code
package com.github.norwae.util
import javax.script.{Invocable, ScriptEngineManager}
object Moo extends App {
val script = "input.sub.structure <= 12"
val escaped = script.replace("'", "\\'")
val factory = new ScriptEngineManager()
package experiment
import akka.actor.ActorSystem
import akka.util.Timeout
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
import spray.http.HttpHeaders.Accept
import spray.http.MediaTypes._
import spray.http.{ContentType, HttpResponse}
import spray.httpx.marshalling.{ToResponseMarshaller, ToResponseMarshallingContext}
@Norwae
Norwae / handler.go
Last active April 4, 2019 14:37
GCP Functions - HandleHTTP
type StatusCodeError struct {
StatusCode int
Message string
}
func (ue *StatusCodeError) Error() string {
return ue.Message
}
var notFound = StatusCodeError{http.StatusNotFound, "The requested URL could not be found"}
@Norwae
Norwae / go.mod
Created April 4, 2019 14:26
Basic go.mod file
module deadmod
go 1.11
require (
github.com/kr/pretty v0.1.0 // indirect
github.com/satori/go.uuid v1.2.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)
@Norwae
Norwae / handler.go
Created April 4, 2019 14:27
Setup Datastore
var store *datastore.Client
func init() {
projectId := os.Getenv("GCP_PROJECT")
client, err := datastore.NewClient(context.Background(), projectId)
if err != nil {
log.Fatalf("Could not create datastore client: %v", err)
}
}
@Norwae
Norwae / handler.go
Created April 4, 2019 14:41
Handler code example
const Kind = "DMT"
type DeadMansTrigger struct {
Id string `json:"id" datastore:",noindex"`
DueToFire time.Time `json:"due"`
HoursBetweenFire int `json:"hoursBetweenFire" datastore:",noindex"`
Checkins []time.Time `json:"checkins" datastore:",noindex"`
FireURL string `json:"fireURL" datastore:",noindex"`
FirePayload string `json:"firePayload" datastore:",noindex"`
}
@Norwae
Norwae / handler.go
Created April 4, 2019 14:42
Setup for the redirect URL
var baseURL string
func init() {
// unfortunately, there seems to be no simple env property reporting the unmangled invocations URL, so
// we build it from its parts
projectId := os.Getenv("GCP_PROJECT")
region := os.Getenv("FUNCTION_REGION")
name := os.Getenv("FUNCTION_NAME")
baseURL = fmt.Sprint("https://", region, "-", projectId, ".cloudfunctions.net/", name)