Skip to content

Instantly share code, notes, and snippets.

View hsanchez's full-sized avatar
I may be slow to respond.

Huascar Sanchez hsanchez

I may be slow to respond.
View GitHub Profile

10 Scala One Liners to Impress Your Friends

Here are 10 one-liners which show the power of scala programming, impress your friends and woo women; ok, maybe not. However, these one liners are a good set of examples using functional programming and scala syntax you may not be familiar with. I feel there is no better way to learn than to see real examples.

Updated: June 17, 2011 - I'm amazed at the popularity of this post, glad everyone enjoyed it and to see it duplicated across so many languages. I've included some of the suggestions to shorten up some of my scala examples. Some I intentionally left longer as a way for explaining / understanding what the functions were doing, not necessarily to produce the shortest possible code; so I'll include both.

1. Multiple Each Item in a List by 2

The map function takes each element in the list and applies it to the corresponding function. In this example, we take each element and multiply it by 2. This will return a list of equivalent size, compare to o

@hsanchez
hsanchez / brewup.sh
Created September 14, 2016 17:26
brewup: upgrading outdated homebrew packages!
#!/bin/bash
# Copyright Huascar Sanchez, 2016.
brew outdated | cut -d ' ' -f 1 | xargs brew upgrade
@hsanchez
hsanchez / GIT-FSCK.adoc
Created February 1, 2016 18:42 — forked from mbbx6spp/GIT-FSCK.adoc
How to check your git object sanity

fsck-ing your Git objects by default

Yo developers (git interactive users), check if you are fsck-ing your objects on transfer:

git config --null --get transfer.fsckobjects
git config --null --get fetch.fsckobjects

If that is null or false, then …​ set it to true:

git config --global transfer.fsckobjects true
@hsanchez
hsanchez / algorithm2e_template
Last active August 29, 2015 14:25 — forked from lgylym/algorithm2e_mapreduce
algorithm template using algorithm2e
\begin{algorithm}[H]
\SetKwFor{ForEach}{for each}{do}{end}%separate foreach with a space
\SetArgSty{textrm} %do not automatically use italic in arguments
\SetKwInOut{KwInit}{Initialization}
\SetKwFunction{Map}{Map}
\SetKwFunction{Reduce}{Reduce}
\SetKwProg{myproc}{Procedure}{}{}
\DontPrintSemicolon % Some LaTeX compilers require you to use \dontprintsemicolon instead
\KwIn{}
\KwOut{}
int m, n;
boolean[][] graph;
boolean seen[];
int matchL[]; //What left vertex i is matched to (or -1 if unmatched)
int matchR[]; //What right vertex j is matched to (or -1 if unmatched)
int maximumMatching() {
//Read input and populate graph[][]
//Set m to be the size of L, n to be the size of R
Arrays.fill(matchL, -1);
@hsanchez
hsanchez / multistage.md
Last active July 12, 2016 09:15
Multistaging of Java Code Examples At StackOverflow

Violette's Multi staging functionality

To evaluate Violette's multi-staging functionality, I have put together the following experiment. Please follow all of the instructions below:

PREREQS

Make sure you have

jquery.scrollLock.js

Useful for when a blocking user experience is needed (in my case, didn't want people unwittingly loosing their place by scrolling while a modal required their attention): $.scrollLock() locks the body in place, preventing scroll until it is unlocked.

// Locks the page if it's currently unlocked
$.scrollLock();

// ...or vice versa
@hsanchez
hsanchez / HowToInstallaScala2-10-4.text
Last active August 29, 2015 14:16
Installing scala 2.10.4 with homebrew
# to install the latest stable version:
brew install scala --with-docs
# to install scala-2.10-4:
brew install https://gist.githubusercontent.com/hsanchez/2cd1844faecb5147ac11/raw/1aa2066cb7e8d6e7ca121316a5aa4360a30b34bf/scala.rb --with-docs
# to switch versions (from https://github.com/mxcl/homebrew/wiki/External-Commands):
brew switch scala 2.10.4
brew switch scala 2.10.3

Stopping a Jekyll server with ctrl-z can cause issues as the process is not stopped fully. To kill it:

$ lsof -wni tcp:4000
$ kill -9 <PID of process>

And next time, use crtl-c to stop.

package com.agilogy.spray.cors
import spray.http.{HttpMethods, HttpMethod, HttpResponse, AllOrigins}
import spray.http.HttpHeaders._
import spray.http.HttpMethods._
import spray.routing._
// see also https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
trait CORSSupport {
this: HttpService =>