Skip to content

Instantly share code, notes, and snippets.

View njbartlett's full-sized avatar

Neil Bartlett njbartlett

View GitHub Profile
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;
}
@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);
}
-- 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
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
module Main where
import Control.Monad
import Data.List
type Line = [Int]
data Wire = Wire { start :: Int
, end :: Int } deriving Show