Skip to content

Instantly share code, notes, and snippets.

View njbartlett's full-sized avatar

Neil Bartlett njbartlett

View GitHub Profile
module Main where
import Control.Monad
import Data.List
type Line = [Int]
data Wire = Wire { start :: Int
, end :: Int } deriving Show
countCrossings :: TestCase -> Int
countCrossings = length . guardedPairs crosses
-- This is the "pairs" function from the first meeting, with the addition of a
-- predicate function to filter out unwanted pairs
guardedPairs :: (a -> a -> Bool) -> [a] -> [(a,a)]
guardedPairs p xs = [(x,y) | (x:ys) <- tails xs, y <- ys, p x y]
crosses :: Wire -> Wire -> Bool
crosses (Wire s1 e1) (Wire s2 e2) = if s1 < s2 then e1 > e2 else e1 < e2
-- Find the longest shared prefix of two lists
-- e.g. findPrefix [1,2,3,4] [1,2,3,5] returns [1,2,3]
findPrefix :: (Eq a) => [a] -> [a] -> [a]
findPrefix xs = map fst . takeWhile (uncurry (==)) . zip xs
This file has been truncated, but you can view the full file.
@njbartlett
njbartlett / Greeting.java
Created October 28, 2010 07:33
Example service interface for bndtools tutorial
package org.example.api;
public interface Greeting {
String sayHello(String name);
}
package org.example.impl;
import org.example.api.Greeting;
import aQute.bnd.annotation.component.Component;
@Component
public class BasicGreeting implements Greeting {
public String sayHello(String name) {
return "Hello " + name;
}
package org.example.impl;
import junit.framework.TestCase;
public class BasicGreetingTest extends TestCase {
public void testHello() {
assertEquals("Hello Fred", new BasicGreeting().sayHello("Fred"));
}
}
package org.example.gui;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
import org.example.api.Greeting;
import aQute.bnd.annotation.component.*;
@Component
@Reference(type = '?')
public void setLog(LogService log) {
// ...
}
public void unsetLog(LogService log) {
// ...
}
@Reference(type = '*')
@njbartlett
njbartlett / gist:1298255
Created October 19, 2011 13:18
JVM stack dump
osgi> 2011-10-18 11:28:36
Full thread dump Java HotSpot(TM) Client VM (14.3-b01 mixed mode):
"[ThreadPool Manager] - Idle Thread" daemon prio=6 tid=0x35668c00 nid=0x1688 in Object.wait() [0x3715f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x07220be8> (a org.eclipse.equinox.internal.util.impl.tpt.threadpool.Executor)
at java.lang.Object.wait(Object.java:485)
at org.eclipse.equinox.internal.util.impl.tpt.threadpool.Executor.run(Executor.java:106)
- locked <0x07220be8> (a org.eclipse.equinox.internal.util.impl.tpt.threadpool.Executor)