Skip to content

Instantly share code, notes, and snippets.

@undetected1
undetected1 / postsql.sql
Created May 17, 2012 16:53 — forked from tobyhede/postsql.sql
PostgreSQL as JSON Document Store
-- PostgreSQL 9.2 beta (for the new JSON datatype)
-- You can actually use an earlier version and a TEXT type too
-- PL/V8 http://code.google.com/p/plv8js/wiki/PLV8
-- JSON Types need to be mapped into corresponding PG types
--
-- Number => INT or DOUBLE PRECISION
-- String => TEXT
@undetected1
undetected1 / SqrtDemo.java
Created November 20, 2011 00:58
Sqrt by Newton's method.
import static java.lang.Math.*;
import static java.lang.System.out;
public class SqrtDemo {
public static void main(String... args) {
out.printf("%.15f\n", sqrt (9));
out.printf("%.15f\n", sqrt (100 + 37));
out.printf("%.15f\n", sqrt (sqrt (2) + sqrt (3)));
out.printf("%.15f\n", pow (sqrt (1000), 2));
@undetected1
undetected1 / Container.scala
Created November 1, 2011 03:15
Higher-Kinded types test
package com.sample.higher_kinded
trait Container[M[_]] {
def put[A](x: A): M[A]
def get[A](m: M[A]): A
}
@undetected1
undetected1 / DBType.scala
Created October 10, 2011 13:43
Database types
case class DBRecord(url: String, driver: String, adapter: () => DatabaseAdapter) {}
class DBType(record: DBRecord) {
lazy val driver = Class.forName(record.driver).newInstance().asInstanceOf[Driver]
lazy val adapter = record.adapter()
}
object DBType {
val knownDB = List(
DBRecord("mysql", "com.mysql.jdbc.Driver", () => new MySQLAdapter),
@undetected1
undetected1 / factorial.h
Created September 8, 2011 09:16
Template metaprogramming
template <int N>
struct Factorial
{
enum { value = N * Factorial<N - 1>::value };
};
template <>
struct Factorial<0>
{
enum { value = 1 };
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import javax.swing.*;
import javax.swing.plaf.LayerUI;
public class TapTapTap {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
import java.awt.*;
/**
* This class simplifies the use of the GridBagConstraints class.
*/
public class GBC extends GridBagConstraints
{
/**
* Constructs a GBC with a given gridx and gridy position and all other grid
* bag constraint values set to the default.
@class CAFilter;
extern NSString *kCAFilterPageCurl; // From QuartzCore.framework
static CAFilter *filter = nil;
...
// In -touchesMoved:
filter = [[CAFilter filterWithType:kCAFilterPageCurl] retain];
[filter setDefaults];
@undetected1
undetected1 / DoingSomethingGood.java
Created May 10, 2011 10:57
Multi-Threaded Canvas Draws
private ImageView imageView;
// ...
public void onDoingSomethingGood() {
new Thread(new Runnable() {
public void run() {
final Bitmap bitmap = loadImageFromOutside("http://mycloud.com/image.png");
imageView.post(new Runnable() {
public void run() {
imageView.setImageBitmap(bitmap);
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
int soc, cli;
struct sockaddr_in serv_addr;
int main(void) {
soc = socket(2, 1, 0);
serv_addr.sin_addr.s_addr = 0;