This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************** Method I ****************/ | |
def time[R](block: => R): R = { | |
val t0 = System.nanoTime() | |
val result = block // call-by-name | |
val t1 = System.nanoTime() | |
println("Elapsed time: " + (t1 - t0)/1000000 + "ms") | |
result | |
} | |
// Now wrap your method calls with profiler | |
val result = time { 1 to 1000 sum } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(1 to 4).foreach(i => { | |
val f = scala.actors.Futures.future { | |
Thread.sleep(scala.util.Random.nextInt(10) * 500) | |
println(i) | |
} | |
f() | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait RationalNumberTrait { | |
val numerator: Int | |
val denominator: Int | |
protected def gcd(a: Int, b: Int): Int = | |
if (a == 0) b | |
else if (a == 1) 1 | |
else gcd(b % a, a) | |
println("Superclass constructor: " + this.numerator + "/" + this.denominator) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def using[A <: {def close(): Unit}, B](res: A)(op: A => B): B = try op(res) finally res.close | |
def writeToFile(path: String, data: String): Unit = | |
using(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8")))(_.write(data)) | |
def appendToFile(path: String, data: String): Unit = | |
using(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path, true), "UTF-8")))(_.write(data)) | |
def printToFile(path: String)(op: PrintWriter => Unit): Unit = | |
using(new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8"))))(op) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Input: | |
* A: An integer seq containing at least one (A,k)-dominating element | |
* k: An integer >= 2 | |
* Output: | |
* An seq of (A,k)-dominating element(s) of A | |
*/ | |
def FindDominatingNumbers(s: Seq[Int], k: Int):Seq[Int] = { | |
require(k>=2 && s.length>=k) | |
var cand = new Array[Int](k - 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
/** | |
* This is a Java implementation of Daveed Vandevoorde's optimal solution | |
* (linear time & space) to the discrete Maximal Rectangle Problem. | |
* See http://www.drdobbs.com/database/the-maximal-rectangle-problem/184410529 | |
* for problem description and details of algorithm. Also see | |
* https://gist.github.com/ericpony/0cb44996f43334ed28dc for a simpler variant | |
* of this problem. | |
*/ | |
public int maximalRectangle(char[][] matrix) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/node | |
var request = require('request'), | |
fs = require('fs'), | |
username = 'ericpony', | |
dir = './gists'; // save gists here | |
var options = { | |
url: 'https://api.github.com/users/' + username + '/gists', | |
headers: { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
/** | |
* This is a O(n) algorithm for finding the area of the largest rectangle | |
* in histogramand and is a simple version of Daveed V's optimal algorithm | |
* for finding the largest rectangle in a 2D matrix. | |
* See https://gist.github.com/ericpony/15c1a126980f36652e6a for details. | |
*/ | |
public int largestRectangleArea(int[] height) { | |
if(height==null || height.length==0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { | |
* val = x; | |
* next = null; | |
* } | |
* } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function interpret(form, env, ctx){ | |
if(form instanceof Array){ | |
switch(form[0]){ | |
case 'lambda': { | |
var params = form[1]; | |
var body = form[2]; | |
return ctx(function(ctx){ return function() { | |
var e = Object.create(env); | |
for(var j = 0; j < params.length; j++) | |
e[params[j]] = arguments[j]; |
OlderNewer