Skip to content

Instantly share code, notes, and snippets.

View Centaur's full-sized avatar

oldpig Centaur

  • singerdream.com
  • Shanghai
View GitHub Profile
@Centaur
Centaur / Gemfile
Last active August 29, 2015 14:28 — forked from petems/Gemfile
An example http download with Progress Bar output in the command line with Ruby and the native `net/http` library...
source "https://rubygems.org"
gem "progressbar"
@Centaur
Centaur / example.scala
Last active August 29, 2015 14:26 — forked from mpilquist/example.scala
Using scala-reflect to desugar
scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._
scala> showCode(reify {
| for{
| x <- 1 to 5
| _ = print("hi")
| } print(x)
| }.tree)
res1: String =
grammar ECMAScript;
options {
// Allow any char but \uFFFF (16 bit -1)
charVocabulary='\u0000'..'\uFFFE';
}
/* this comes from section A.5 but is really the starting point, so
* it is present here.
*/
program:
object Constants {
val MaterialSep = ","
}
case class Client(need: Option[String], product: Option[String])
def optionsIntoMap(x1: (String, Option[String]),
x2: (String, Option[String])): Map[String, List[String]] = {
List(x1, x2).collect {
@Centaur
Centaur / p2.scala
Last active August 29, 2015 14:25
parboiled2
package com.gtan.parsers
import org.parboiled2._
import shapeless._
/**
* val MavenFormat = """(/.+)+/((.+?)(_(.+?)(_(.+))?)?)/(.+?)/(\3-\8(-(.+?))?\.(.+))""".r
* "/org/scala-lang/modules/scalajs/scalajs-sbt-plugin_2.10_0.13/0.5.6/scalajs-sbt-plugin-0.5.6.pom"
* "/io/spray/sbt-revolver_2.10_0.13/0.7.2/sbt-revolver-0.7.2.pom"
* "/org/scala-lang/modules/scalajs/scalajs-tools_2.10/0.5.6/scalajs-tools_2.10-0.5.6.pom"

计算一组Range所覆盖的不重复整数的个数,求 < O(n*n) 的算法。

def cover(rs: Range*):Int = ???


assert(cover(0 to 4, 1 to 5) == 6)
assert(cover(0 to 4, 1 to 3) == 5)
assert(cover(0 to 4, 6 to 7) == 7)
assert(cover(0 to 4, 6 to 7, 2 to 6) == 8)
@Centaur
Centaur / answer.md
Last active August 29, 2015 14:20 — forked from non/answer.md

What is the appeal of dynamically-typed languages?

Kris Nuttycombe asks:

I genuinely wish I understood the appeal of unityped languages better. Can someone who really knows both well-typed and unityped explain?

I think the terms well-typed and unityped are a bit of question-begging here (you might as well say good-typed versus bad-typed), so instead I will say statically-typed and dynamically-typed.

I'm going to approach this article using Scala to stand-in for static typing and Python for dynamic typing. I feel like I am credibly proficient both languages: I don't currently write a lot of Python, but I still have affection for the language, and have probably written hundreds of thousands of lines of Python code over the years.

def bubbleSort[A: Ordering](xs: List[A]): List[A] = {
@annotation.tailrec
def bubble(ys: List[A], init: List[A], max: A): (List[A], A) = ys match {
case Nil => (init, max)
case h :: tail =>
if(implicitly[Ordering[A]].compare(h, max) > 0)
bubble(tail, max::init, h)
else
@annotation.tailrec
def isSorted[A](as: Seq[A], ordered: (A, A)=>Boolean):Boolean = {
as match {
case Seq() => true
case Seq(_) => true
case Seq(head, second, rest@_*) =>
ordered(head, second) && isSorted(second +: rest, ordered)
}
}
/*
* Copyright (C) 2013 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software