Skip to content

Instantly share code, notes, and snippets.

@mandykoh
mandykoh / refizzbuzz.rb
Created February 14, 2019 12:05
FizzBuzz with Regexes
#!/usr/bin/env ruby
@parts = ''
(0...100).each { |i| @parts += ',' + (i + 1).to_s }
def mark(n, label)
@parts.gsub! /((,\w+){#{n - 1}})(,\w+)/, "\\1,#{label}"
end
mark 3, 'Fizz'
@mandykoh
mandykoh / refizzbuzz.go
Last active February 14, 2019 11:47
FizzBuzz With Regexes
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
)
func main() {
@mandykoh
mandykoh / dct.go
Created April 9, 2017 21:56
Generalised 2D Type-II DCT for MxN matrix
package dct
import "math"
func DCT(width int, height int, values []float64) (result []float64) {
doubleWidth := 2.0 * float64(width)
doubleHeight := 2.0 * float64(height)
result = make([]float64, len(values))
@mandykoh
mandykoh / SymbolicLock.java
Created May 13, 2013 20:49
Class that provides a version of 'synchronized' which locks on a string value rather than an object instance.
import java.util.HashMap;
import java.util.Map;
public class SymbolicLock
{
private final Map<String, LockEntry> lockEntries = new HashMap<String, LockEntry>();
public void synchronised(String key, Runnable runnable)
{
var stream = /* Some System.IO.Stream */
using (var contentSource = new ChunkedStreamSerialiser(stream)) {
var response = new HttpResponse(HttpStatuses.OK, "OK");
response.Headers.Set("Content-Type", "application/octet-stream");
response.Body = contentSource;
/* Do something with the response ... */
}
var content = Encoding.UTF8.GetBytes("Hello, World!");
using (var contentSource = new ByteArraySource(content)) {
var response = new HttpResponse(HttpStatuses.OK, "OK");
response.Headers.Set("Content-Type", "text/plain");
response.Body = contentSource;
/* Do something with the response ... */
}
@mandykoh
mandykoh / HttpListenerExample.cs
Created April 10, 2010 13:19
HttpListener example
using System.Text;
using System.Threading;
using Naucera.Io.Http;
using Naucera.Net.Http;
public class HttpListenerExample
{
public static void Main(string[] args)
{
using (var listener = new HttpListener(8080, HandleRequest)) {