Skip to content

Instantly share code, notes, and snippets.

View ScalaWilliam's full-sized avatar
🏠
Working from home

ScalaWilliam ScalaWilliam

🏠
Working from home
View GitHub Profile
@ScalaWilliam
ScalaWilliam / Output.xml
Last active April 6, 2018 17:18
Automatic XML printing for case classes in Scala. Tested with Scala 2.10.3.
<Human>
<title>Mr.</title>
<name>Johnny</name>
<coats>
<Coat>
<brand>BOSS</brand>
</Coat>
<Coat>
<brand xsi:nil="true"/>
</Coat>
@ScalaWilliam
ScalaWilliam / a-input.xml
Last active December 28, 2015 17:29
Introducing recursion and then flattening the result with Saxon. Desire: do section numbering and grouping without using counters. Platform: Saxon 6.5.5
<?xml version="1.0" encoding="UTF-8"?>
<root>
<heading>Hello</heading>
<paragraph>This is my first paragraph</paragraph>
<paragraph>This is my second paragraph</paragraph>
<heading>Hello!</heading>
<heading>HelloOooo!</heading>
<paragraph>Coolness.</paragraph>
</root>
@ScalaWilliam
ScalaWilliam / scala-project.bat
Created November 19, 2013 09:41
Put this in: C:\Programs..\Scala\bin\ Then run scala-project com.something.great Will create a scala project names 'great' in group 'com.something'. Depends on: https://github.com/davidB/scala-archetype-simple/
@echo off
setlocal EnableDelayedExpansion
set "groupId="
set "rest=%1"
set "f="
if "%rest" == "" goto bad
:bang
for /f "tokens=1* delims=." %%p in ("%rest%") DO (
scala> val myMap = Map("test"->"best")
myMap: scala.collection.mutable.Map[String,String] = Map(test -> best)
scala> val fn = myMap.lift
fn: String => Option[String] = <function1>
scala> (fn("test"), fn("lest"))
res5: (Option[String], Option[String]) = (Some(best),None)
scala> myMap("lest") = "zest"
@ScalaWilliam
ScalaWilliam / pairHeadingsParagraphs.scala
Created December 26, 2013 00:00
pairHeadingsParagraphs.scala
def packer[A](l: List[A])(f: A => Boolean): List[(A, List[A])] = {
if ( l.isEmpty ) List()
else if ( !f(l.head) ) List()
else {
val heading :: rest = l
val (paragraphs, nextBunch) = rest span (false == f(_))
(heading, paragraphs) :: packer(nextBunch)(f)
}
}
case class Heading(title: String)
@ScalaWilliam
ScalaWilliam / groupSiblings.scala
Last active January 1, 2016 09:59
Group consecutive siblings according to truth/falsity. Useful to interpret something like parsing output from the shell to distinguish between Input and Output.
def continuousPartitions[A](l: List[A])(f: A => Boolean): List[(List[A], List[A])] = {
if ( l.isEmpty ) Nil
else {
val (continuousTrue, followingPartition) = l span f
val (continuousFalse, nextGroup) = followingPartition span (!f(_))
(continuousTrue, continuousFalse) :: continuousPartitions(nextGroup)(f)
}
}
val hav = continuousPartitions(List("a", "b", "a", "a", "b", "b", "a"))(_=="a")
@ScalaWilliam
ScalaWilliam / remove-linkedin-irrelevant-content.js
Last active August 29, 2015 13:56
Would be nice if Premium LinkedIn did not include too much irrelevant content. I only care about relevant content.
// ==UserScript==
// @name NoPulses
// @namespace https://www.linkedin.com/
// @include https://www.linkedin.com/*
// @version 1
// @grant none
// ==/UserScript==
(function() {
function removeNonsense() {
@ScalaWilliam
ScalaWilliam / gist:9711648
Last active July 19, 2016 21:03
mg7150 linux (ubuntu 13.10, fedora, debian, redhat, centos, source) drivers
http://support-sg.canon-asia.com/P/search?model=PIXMA+MG7170&menu=download&filter=0&tagname=g_os&g_os=Linux
also deutsch http://aram.loosman.ch/canon-pixma-mg7150-und-ubuntu-13-04/
@ScalaWilliam
ScalaWilliam / Doubles.scala
Last active August 29, 2015 14:00
Adding up XML as doubles. Shows a case for Monads
package com.scalawilliam.examples
import scala.xml.Node
/**
* Add .sum and .product functionality to Scala XML nodes.
*/
object NumericNodesExample extends App {
import NumericUtility._
@ScalaWilliam
ScalaWilliam / initialise.scala
Last active August 29, 2015 14:04
Embedded Jetty + Scalatra with Webjars loading (loads META-INF/resources data from the classpath jars). Works without any configuration in Jetty, jetty-maven-plugin, but not so easily in embedded form. Make sure to create websresources/index.html in your src/main/resources/ directory. Examples provided in Scalatra documentation are strange - set…
def initialiseApp[T<:LifeCycle](port: Int) = {
val server = new Server(port)
val context = new WebAppContext()
context.setContextPath("/")
context.setResourceBase(new File(getClass.getResource("/webresources/index.html").getFile).getCanonicalFile.getParentFile.getCanonicalPath)
context.addEventListener(new ScalatraListener)
context.addServlet(classOf[DefaultServlet], "/")
context.setInitParameter(ScalatraListener.LifeCycleKey, classOf[ConfiguredBootstrap].getCanonicalName)
context.setAttribute(WebInfConfiguration.WEBINF_JAR_PATTERN, ".*\\.jar$")