Skip to content

Instantly share code, notes, and snippets.

View Allan-Gong's full-sized avatar

Allan Gong Allan-Gong

  • Facebook
  • Seattle, US
View GitHub Profile
@Allan-Gong
Allan-Gong / currying.scala
Last active November 30, 2015 04:40
Scala - difference between multiple parameter lists and a single list of parameters
// Reference: http://stackoverflow.com/questions/6803211/whats-the-difference-between-multiple-parameters-lists-and-multiple-parameters
// 1. The multiple arguments lists allow the method to be used in the place of a partially applied function:
object NonCurr {
def tabulate[A](n: Int, fun: Int => A) = IndexedSeq.tabulate(n)(fun)
}
NonCurr.tabulate[Double](10, _) // not possible
val x = IndexedSeq.tabulate[Double](10) _ // possible. x is Function1 now
x(math.exp(_))
@Allan-Gong
Allan-Gong / dispatch.scala
Last active November 30, 2015 04:52
How to use databinder dispatch to invoke HTTP request against a web server with self-signed certificate ?
import com.ning.http.client._
import dispatch._, Defaults._
val requestTimeoutInMs = 5000
val allowSelfSignedSSLCertificate = true
val builder = new AsyncHttpClientConfig.Builder()
builder
.setAllowPoolingConnections(true)
.setRequestTimeout(requestTimeoutInMs)
@Allan-Gong
Allan-Gong / lift_language_bundle.scala
Last active December 2, 2015 04:07
Scala Lift: How to move language bundles out of the default resource folder (src/main/resource)
// Default way of loading language bundle in Scala Lift
// Relying on language bundle file(s) to be found in src/main/resources/
// LiftRules.resourceNames = List("languageBundle_en_AU")
val appConfDir = System.getProperty("appConfigDirectory")
LiftRules.resourceBundleFactories.prepend {
case (key, locale) => {
val file = new File(appConfDir)
val urls = Array(file.toURI.toURL)
val loader = new URLClassLoader(urls)
@Allan-Gong
Allan-Gong / colon_underscore_star.scala
Last active November 30, 2015 23:56
What does ':_*' (colon underscore star) do in Scala
// :*_ tells the compiler to pass each element of arr as its own argument to echo, rather than all of it as a single argument.
// It is useful when converting list to Map in Scala
var mutableMapInstanceOfItemToBoolean = scala.collection.mutable.Map(arrayOfSomeSort.values.toList.map(item => item -> false):_*)
@Allan-Gong
Allan-Gong / Crypt_TripleDES.pl
Last active December 1, 2015 00:18
Encrypts the plaintext string using the passphrase. Whitespace characters are appended to the string if its length is not a multiple of eight. User applications can correct for this by storing plaintext size with the cyphertext. The passphrase is an ASCII character string of upto 48 characters.
###################################
#########Encrypt example###########
###################################
use Data::Random qw( rand_chars );
use Crypt::TripleDES;
my $password = q{password};
my $password_len = length $password;
@Allan-Gong
Allan-Gong / implicit.scala
Last active December 10, 2015 05:42
Demonstrate implicit in Scala
/*******************************/
/***** Implicit conversion *****/
/*******************************/
// If one calls a method m on an object o of a class C, and that class does not support method m,
// then Scala will look for an implicit conversion from C to something that does support m.
// Use to automatically convert a value from one type to another
val value:BigInt = 1
// What actually happened is:
@Allan-Gong
Allan-Gong / closure.scala
Created December 10, 2015 05:52
Demonstration of closure in Scala
package otherscope {
class Foo {
// a method that takes a function and a string, and passes the string into
// the function, and then executes the function
def exec(f:(String) => Unit, name: String) {
f(name)
}
}
}
@Allan-Gong
Allan-Gong / aws_wes.pl
Last active July 29, 2018 15:26
An example of installing Perl via perlbrew, Apache with mod_perl and a bunch of perl modules on AWS EC2 Red Hat Linux Enterprise
sodo yum install perl
sudo yum install bzip2
sudo yum install patch
sudo yum groupinstall 'Development Tools'
\curl -L http://install.perlbrew.pl | bash
@Allan-Gong
Allan-Gong / di.scala
Created January 12, 2016 23:21
Dependency Injection in Scala
// cake pattern with self-type annotation
// =======================
// service interfaces
trait OnOffDeviceComponent {
val onOff: OnOffDevice
trait OnOffDevice {
def on: Unit
def off: Unit
}
@Allan-Gong
Allan-Gong / context_bound.scala
Created May 3, 2016 06:09
Context bound in scala
// Consider the method tabulate which forms an array from the results of applying a given function f on a range of numbers from 0 until a given length.
// Up to Scala 2.7, tabulate could be written as follows:
def tabulate[T](len: Int, f: Int => T) = {
val xs = new Array[T](len)
for (i <- 0 until len) xs(i) = f(i)
xs
}
// In Scala 2.8 this is no longer possible, because runtime information is necessary to create the right representation of Array[T].