")
12 | }
13 | val directory = args(0)
14 | var iter = new StreamIterator("ls "+directory lines_!)
15 | println("name,symbol,latitude,longitude")
16 | while(iter.hasNext) {
17 | var filename = iter.next
18 | var jpegFile = new ExifInfo(directory+File.separator+filename)
19 | for (dir <- jpegFile; if dir.name.equals("GPS")) println(dir.toString)
20 | }
21 | }
22 | }
--------------------------------------------------------------------------------
/s4/Exif-Extractor/daori/src/main/scala/pis/chap23/ExifInfo.scala:
--------------------------------------------------------------------------------
1 | package pis.chap23
2 |
3 | import java.io.File
4 | import com.drew.metadata.Metadata
5 | import com.drew.imaging.ImageMetadataReader
6 | import com.drew.metadata.Directory
7 | import com.drew.metadata.Tag
8 | import scala.collection.JavaConversions._
9 | import com.drew.metadata.exif.GpsDirectory
10 | import com.sun.jdi.FloatValue
11 |
12 | class ExifInfo(var path: String) {
13 | var jpegFile = new File(path);
14 | var metadata = ImageMetadataReader.readMetadata(jpegFile);
15 |
16 | def map[T](f: ExifDirectory => T) = {
17 | var result = List[T]()
18 |
19 | for (dir <- metadata.getDirectories) {
20 | result = f( new ExifDirectory(dir)) :: result
21 | }
22 | result
23 | }
24 |
25 | def foreach(b: ExifDirectory => Unit) {
26 | for (dir <- metadata.getDirectories) {
27 | b(new ExifDirectory(dir))
28 | }
29 | }
30 |
31 | def withFilter(p: ExifDirectory => Boolean) = {
32 | var result = List[ExifDirectory]()
33 | for (dir <- metadata.getDirectories) {
34 | val exifDir = new ExifDirectory(dir)
35 | if (p(exifDir)) {
36 | result = exifDir :: result
37 | }
38 | }
39 |
40 | result
41 | }
42 |
43 | class ExifTag(val tag: Tag) {
44 | override def toString() = tag.toString
45 | }
46 |
47 | class ExifDirectory(val dir: Directory) {
48 | def map[T](f: ExifTag => T) = {
49 | var result = List[T]()
50 |
51 | for (tag <- dir.getTags) {
52 | result = f(new ExifTag(tag)) :: result
53 | }
54 | result
55 | }
56 |
57 | def name = dir.getName
58 |
59 | def tags = dir.getTags
60 |
61 | def foreach(b: ExifTag => Unit) {
62 | for (tag <- dir.getTags) {
63 | b( new ExifTag(tag))
64 | }
65 | }
66 |
67 | def latitude = {
68 | val d = dir.getRationalArray(GpsDirectory.TAG_GPS_LATITUDE)(0).floatValue
69 | val m = dir.getRationalArray(GpsDirectory.TAG_GPS_LATITUDE)(1).floatValue/60
70 | val s = dir.getRationalArray(GpsDirectory.TAG_GPS_LATITUDE)(2).floatValue/60/60
71 | d + m + s
72 | }
73 |
74 | def longitude = {
75 | val d = dir.getRationalArray(GpsDirectory.TAG_GPS_LONGITUDE)(0).floatValue
76 | val m = dir.getRationalArray(GpsDirectory.TAG_GPS_LONGITUDE)(1).floatValue/60
77 | val s = dir.getRationalArray(GpsDirectory.TAG_GPS_LONGITUDE)(2).floatValue/60/60
78 | d + m + s
79 | }
80 |
81 | override def toString() = path+",camera,"+this.latitude+","+this.longitude
82 | }
83 | }
--------------------------------------------------------------------------------
/s4/Exif-Extractor/zeider/ExifExtractor.scala:
--------------------------------------------------------------------------------
1 | package chap23
2 |
3 | import sys.process._
4 | import java.io.File
5 | import com.drew.imaging.ImageMetadataReader
6 | import com.drew.metadata.Metadata
7 | import com.drew.metadata.exif.GpsDirectory
8 | import com.drew.metadata.exif.GpsDescriptor
9 | import com.drew.metadata.Directory
10 | import scala.collection.JavaConversions._
11 |
12 | class ExifExtractor {
13 |
14 | //read file
15 | def readFile(path:String) = {
16 | ("find " + path + " -type f -name *.JPG -or -name *.jpg").lines_!
17 | }
18 |
19 | //get exif
20 | def getExif(files:Stream[String]):Stream[Metadata] = {
21 | files map (f => ImageMetadataReader.readMetadata(new File(f)))
22 | }
23 |
24 | //get directory
25 | def getDirectory(metadatas:Stream[Metadata]):List[(String, String)] = {
26 | for(data <- metadatas) {
27 | getTags(data.getDirectories())
28 | }
29 |
30 | var list = List[(String, String)]();
31 |
32 | for(data <- metadatas) {
33 | list = getDescription(data) :: list
34 | }
35 |
36 | list
37 | }
38 |
39 | def getTags(directories:Iterable[Directory]) = {
40 | directories flatMap (directory => getTag(directory))
41 | }
42 |
43 | def getTag(directory:Directory) = {
44 | directory.getTags() withFilter(tag => tag.getTagName() startsWith "GPS" ) map (tag => println(tag))
45 | }
46 |
47 | def getDescription(metadata:Metadata):(String, String) = {
48 | val dir = metadata.getDirectory(classOf[GpsDirectory])
49 | val descriptor = new GpsDescriptor(dir)
50 |
51 | (descriptor.getGpsLatitudeDescription(), descriptor.getGpsLongitudeDescription())
52 | }
53 |
54 |
55 | }
--------------------------------------------------------------------------------
/s4/TagHoister/daori/build.sbt:
--------------------------------------------------------------------------------
1 | name := "TagHoister"
2 |
3 | version := "0.4"
4 |
5 | scalaVersion := "2.9.1"
6 |
7 | libraryDependencies += "junit" % "junit" % "4.9" % "test"
8 |
9 | libraryDependencies += "org.scalatest" %% "scalatest" % "1.7.2" % "test"
10 |
11 | libraryDependencies += "org.scalaz" %% "scalaz-core" % "6.0.4"
12 |
13 | libraryDependencies += "net.liftweb" % "lift-json-ext_2.9.1" % "2.4"
14 |
15 | /* add project library ('groupId % artifactId % version' seperated by comma) */
16 | libraryDependencies ++= Seq()
17 |
--------------------------------------------------------------------------------
/s4/TagHoister/daori/project/plugins.sbt:
--------------------------------------------------------------------------------
1 | resolvers += Classpaths.typesafeSnapshots
2 |
3 | addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0-SNAPSHOT")
--------------------------------------------------------------------------------
/s4/TagHoister/daori/src/main/scala/com/daori/taghoister/Bookmark.scala:
--------------------------------------------------------------------------------
1 | package com.daori.taghoister
2 |
3 | import java.util.Date
4 | case class Bookmark(val url: String, val user:String, created_at:String, title:String, tags:List[String], id:String) {
5 |
6 | def tagCombination = {
7 |
8 | def tagCom( targetTags:List[String]):List[(String,String)] = {
9 | targetTags match {
10 | case Nil => Nil
11 | case List(val1) => Nil
12 | case List(val1, val2) => List((val1, val2))
13 | case _ =>
14 | {
15 | val head = targetTags.head
16 | val tails = targetTags.tail
17 |
18 | var result:List[(String,String)]= Nil
19 |
20 | for( tailTag <- tails) {
21 | result = (head, tailTag) :: result
22 | }
23 |
24 | result ++ tagCom( tails)
25 | }
26 |
27 | }
28 | }
29 |
30 | tagCom( tags.sorted).toSet
31 | }
32 | }
33 |
34 | case class BookmarkContainer(bookmarks:List[Bookmark]) {
35 |
36 | }
--------------------------------------------------------------------------------
/s4/TagHoister/daori/src/main/scala/com/daori/taghoister/LasMultiMap.scala:
--------------------------------------------------------------------------------
1 | package com.daori.taghoister
2 |
3 | import scala.collection.mutable.HashMap
4 |
5 | class LasMultiMap[K, V] {
6 |
7 | var map:HashMap[K, List[V]] = new HashMap[K, List[V]]
8 |
9 | def update(key: K, value: V) {
10 | if (map.contains(key)) {
11 | map.put(key, map.get(key).get :+ value)
12 | } else {
13 | map.put(key, value :: Nil)
14 | }
15 | }
16 |
17 | def apply(key: K): Option[List[V]] = {
18 | map.get(key)
19 | }
20 |
21 | def put(key:K, value:V) = {
22 | update(key, value)
23 | }
24 |
25 | def keySet():scala.collection.Set[K] = {
26 | map.keySet
27 | }
28 | }
--------------------------------------------------------------------------------
/s4/TagHoister/daori/src/main/scala/com/daori/taghoister/Main.scala:
--------------------------------------------------------------------------------
1 | package com.daori.taghoister
2 |
3 | import java.io.BufferedReader
4 | import java.io.FileReader
5 | import net.liftweb.json.JsonParser
6 | import net.liftweb.json.DefaultFormats
7 | import scala.collection.immutable.Set
8 | import java.io.File
9 | import java.io.FileWriter
10 |
11 | object Main extends App {
12 |
13 | def printAll(m: TagHoister):String = {
14 | val sb: StringBuilder = new StringBuilder
15 | for (r <- m.mmap.map) {
16 | if (r._2.size >= 2) sb.append(decorate(r._1, r._2))
17 | }
18 | return sb.toString
19 | }
20 |
21 | def print(m: TagHoister, keys: Array[String]):String = {
22 | val sortedKeys = keys.sorted
23 | decorate((sortedKeys(0), sortedKeys(1)), m.mmap((sortedKeys(0), sortedKeys(1))).get)
24 | }
25 |
26 | def decorate(tags:(String, String), list:List[Bookmark]):String = {
27 | val sb: StringBuilder = new StringBuilder
28 | sb.append("")
33 | sb.append("" + tags._1 + ", " + tags._2 + "
")
34 | sb.append("
")
35 | return sb.toString
36 | }
37 |
38 | def tags(m: TagHoister) = {
39 | m.mmap.keySet
40 | }
41 |
42 | override def main(args: Array[String]) = {
43 | val m: TagHoister = new TagHoister
44 | var result = ""
45 | if (args.length == 0) result = printAll(m)
46 | else result = print(m, args)
47 | var file = new File("output.html")
48 | var fileW = new FileWriter(file)
49 | fileW.write(result)
50 | fileW.close
51 | }
52 | }
--------------------------------------------------------------------------------
/s4/TagHoister/daori/src/main/scala/com/daori/taghoister/TagHoister.scala:
--------------------------------------------------------------------------------
1 | package com.daori.taghoister
2 |
3 | import net.liftweb.json.JsonParser
4 | import java.io.BufferedReader
5 | import net.liftweb.json.DefaultFormats
6 | import java.io.FileReader
7 |
8 | class TagHoister {
9 | val mmap = new LasMultiMap[(String, String), Bookmark]()
10 |
11 | implicit val formats = DefaultFormats
12 |
13 | val reader = new BufferedReader(new FileReader("diigo-bookmark.1000.type1.json"))
14 | val data = new StringBuilder
15 | var line = reader.readLine
16 |
17 | while (line != null) {
18 | data append line
19 | line = reader.readLine
20 | }
21 | val jsons = JsonParser.parse(data.toString)
22 |
23 | val bookmarks = jsons.extract[List[Bookmark]]
24 |
25 | for (bookmark <- bookmarks) {
26 | for (tagCombination <- bookmark.tagCombination) {
27 | mmap.put(tagCombination, bookmark)
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/s4/tagHoister/zeider/build.sbt:
--------------------------------------------------------------------------------
1 | name := "my-scala-project"
2 |
3 | version := "0.4"
4 |
5 | scalaVersion := "2.9.2"
6 |
7 | libraryDependencies += "junit" % "junit" % "4.9" % "test"
8 |
9 | libraryDependencies += "org.scalatest" %% "scalatest" % "1.7.2" % "test"
10 |
11 | libraryDependencies += "org.scalaz" %% "scalaz-core" % "6.0.4"
12 |
13 | /* add project library ('groupId % artifactId % version' seperated by comma) */
14 | libraryDependencies ++= Seq()
15 |
--------------------------------------------------------------------------------
/s4/tagHoister/zeider/project/plugins.sbt:
--------------------------------------------------------------------------------
1 | resolvers += Classpaths.typesafeSnapshots
2 |
3 | addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0-SNAPSHOT")
--------------------------------------------------------------------------------
/s4/tagHoister/zeider/src/main/scala/pis/chap24/zeider/TagHoister.scala:
--------------------------------------------------------------------------------
1 | package pis.chap24.zeider
2 |
3 | import java.net.URL
4 | import scala.io.Source
5 | import scala.util.parsing.json._
6 |
7 | class TagHoister {
8 | def extractAllTags(bookmarks:List[Bookmark]) = {
9 | println("extractAllTags")
10 | bookmarks flatMap (bookmark => bookmark.tags)
11 | }
12 | def removeDuplicatedTags(bookmarks:List[Bookmark]) = {
13 | println("removeDuplicatedTags")
14 | bookmarks flatMap (x => x.tags) toSet
15 | }
16 | def findTagsTwoMoreUsed(bookmarks:List[Bookmark]) = {
17 | println("findTagsTwoMoreUsed")
18 | val tags = extractAllTags(bookmarks)
19 | val uniqueTags = removeDuplicatedTags(bookmarks)
20 | println(uniqueTags.size)
21 | uniqueTags map (x => tags filter (y => y == x)) filter (z => z.length > 1) flatMap ( m => m.toSet )
22 | }
23 | def findCandidateTagGroup(bookmarks:List[Bookmark]) = {
24 | println("findCandidateTagGroup")
25 | val tags = findTagsTwoMoreUsed(bookmarks)
26 | tags flatMap (x => Set(x) zipAll(tags, x, null)) map (y => Set(y._1, y._2)) filter (z => z.size > 1) toList
27 | }
28 | def findNotTheSameButSimilarGroup(bookmarks:List[Bookmark]) = {
29 | println("findNotTheSameButSimilarGroup")
30 | val tagGroup = findCandidateTagGroup(bookmarks)
31 | (tagGroup.view).par flatMap (x => bookmarks map ( y => (x & y.tags.toSet, y))
32 | filter (z => z._1.size > 1)) groupBy (m => m._1) map (l => Map(l._1 -> (l._2 map (n => n._2)).toSet)) flatMap (b => b) filter (c => c._2.size > 1) toMap
33 | }
34 |
35 | def convert(jsonData:String):List[Bookmark] = {
36 |
37 | JSON.parseRaw(jsonData) match {
38 |
39 | case jsonObject:Some[JSONObject] => {
40 |
41 | jsonObject.get.obj.get("results") match {
42 | case jsonArray:Some[JSONArray] => {
43 | jsonArray.get.list.map(value => {
44 | val jsonBookmark = value.asInstanceOf[JSONObject].obj;
45 |
46 | val id = jsonBookmark("id").toString()
47 | val user = jsonBookmark("user").toString()
48 | val title = jsonBookmark("title").toString()
49 | val url = jsonBookmark("url").toString()
50 | val tags = jsonBookmark("tags").asInstanceOf[JSONArray]
51 | .list.map(obj => obj.toString())
52 |
53 | Bookmark(id, user, title, url, tags)
54 | })
55 | }
56 | case None => Nil
57 | }
58 |
59 | }
60 | case None => Nil
61 | }
62 | }
63 | }
64 |
65 | object TagHoister extends App {
66 | println("started")
67 | val url = new URL("https://raw.github.com/gist/2900472/4ff97a0c416d37a14f4224b24895b0224c3e335c/bookmark.json")
68 | println("get Url")
69 | val jsonData = Source.fromURL(url).mkString
70 | var th = new TagHoister
71 | println("convert")
72 | val bookmarks = th convert jsonData
73 | println("solving... plz wait!")
74 | val result = th findNotTheSameButSimilarGroup bookmarks
75 | result map (x => {
76 | println("> Tags [" + (x._1 map (y => y)) + "]")
77 | x._2 map (bookmark => println("- " + bookmark))
78 | println("-----------")
79 | })
80 | }
81 |
82 | case class Bookmark(id:String, user:String, title:String, url:String, tags:List[String]) {
83 | override def toString() = {
84 | url + "/" + tags
85 | }
86 | }
--------------------------------------------------------------------------------
/s4/tagHoister/zeider/src/test/SAMPLE-TEST:
--------------------------------------------------------------------------------
1 | sample test cases included in pis package.
--------------------------------------------------------------------------------
/s4/tagHoister/zeider/src/test/scala/pis/chap24/zeider/TagHoisterSpec.scala:
--------------------------------------------------------------------------------
1 | package pis.chap24.zeider
2 |
3 | import org.scalatest.Spec
4 | import org.scalatest.BeforeAndAfter
5 |
6 | class TagHoisterSpec extends Spec with BeforeAndAfter {
7 | var th:TagHoister = null
8 | var bookmarks:List[Bookmark] = List.empty
9 | var a:Bookmark = null
10 | var b:Bookmark = null
11 | var c:Bookmark = null
12 |
13 | before {
14 | th = new TagHoister
15 | a = new Bookmark("outsider", "user", "user", "http://google.com", List("search", "google", "engine"))
16 | b = new Bookmark("outsider", "user", "user", "http://daum.com", List("search", "daum", "portal", "engine"))
17 | c = new Bookmark("outsider", "user", "user", "http://naver.com", List("search", "naver", "portal"))
18 |
19 | bookmarks = List(a, b, c)
20 | }
21 |
22 | describe("Tag 추출") {
23 | it("북마크리스트에서 전체 태그를 추출한다") {
24 | val tags = th.extractAllTags(bookmarks)
25 | assert(tags === List("search", "google", "engine", "search", "daum", "portal", "engine", "search", "naver", "portal"))
26 | }
27 | it("전체태그에서 중복을 제거한다") {
28 | val tags = th.removeDuplicatedTags(bookmarks)
29 | assert(tags === Set("google", "engine", "search", "daum", "portal", "naver"))
30 | }
31 | it("전체 태그에서 2번이상 사용된 유니크 태그를 추출한다") {
32 | val tags = th.findTagsTwoMoreUsed(bookmarks)
33 | assert(tags === Set("search", "portal", "engine"))
34 | }
35 | it("태그그룹 후보군 찾기") {
36 | val tagGroup = th.findCandidateTagGroup(bookmarks)
37 | assert(tagGroup === List(Set("engine", "search"), Set("search", "portal"), Set("portal", "engine")))
38 | }
39 | it("태그그룹별 소속된 북마크 찾기") {
40 | val bookmarkGroup = th.findNotTheSameButSimilarGroup(bookmarks)
41 | assert(bookmarkGroup === Map(Set("engine", "search") -> Set(a, b), Set("search", "portal") -> Set(b, c)))
42 | }
43 | }
44 |
45 | }
--------------------------------------------------------------------------------
/sbestudy/anarcher/ch10/queens.scala:
--------------------------------------------------------------------------------
1 | def queens(n: Int): List[List[Int]] = {
2 | //FIXME:
3 | def isSafe(col: Int, queens: List[Int], delta: Int): Boolean = false
4 | def placeQueens(k: Int): List[List[Int]] =
5 | if (k == 0) List(List())
6 | else for {
7 | queens <- placeQueens(k - 1)
8 | column <- List.range(1, n + 1)
9 | if isSafe(column, queens, 1) } yield column :: queens
10 | placeQueens(n)
11 | }
12 |
13 | // vim: set ts=4 sw=4 et:
14 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/java/adder/src/net/javajigi/adder/Action.java:
--------------------------------------------------------------------------------
1 | package net.javajigi.adder;
2 |
3 | public interface Action {
4 | void action();
5 | }
6 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/java/adder/src/net/javajigi/adder/Simulation.java:
--------------------------------------------------------------------------------
1 | package net.javajigi.adder;
2 |
3 | import java.util.concurrent.CopyOnWriteArrayList;
4 |
5 | import net.javajigi.adder.action.AndGateAction;
6 | import net.javajigi.adder.action.InverterAction;
7 | import net.javajigi.adder.action.OrGateAction;
8 |
9 | public class Simulation {
10 | private Integer currentTime = 0;
11 |
12 | private CopyOnWriteArrayList agenda = new CopyOnWriteArrayList();
13 |
14 | public Integer getCurrentTime() {
15 | return currentTime;
16 | }
17 |
18 | public void addWireListener(WireListener listener) {
19 | agenda.add(new WorkItem(currentTime+listener.getDelayTime(), listener));
20 | }
21 |
22 | public void run() {
23 | for (WorkItem workItem : agenda) {
24 | WireListener listener = workItem.getListener();
25 | currentTime = workItem.getTime();
26 | listener.listen();
27 | }
28 | }
29 |
30 |
31 | private class WorkItem {
32 | private int time;
33 | private WireListener listener;
34 |
35 | public WorkItem(int time, WireListener listener) {
36 | this.time = time;
37 | this.listener = listener;
38 | }
39 |
40 | public int getTime() {
41 | return time;
42 | }
43 |
44 | public WireListener getListener() {
45 | return listener;
46 | }
47 | };
48 |
49 |
50 | public void halfAdder(Wire input1, Wire input2, Wire sum, Wire carry) {
51 | Wire i = new Wire();
52 | Wire j = new Wire();
53 |
54 | Action orGate = new OrGateAction(input1, input2, i, this);
55 | orGate.action();
56 |
57 | Action andGate1 = new AndGateAction(input1, input2, carry, this);
58 | andGate1.action();
59 |
60 | Action inverter = new InverterAction(carry, j, this);
61 | inverter.action();
62 |
63 | Action andGate2 = new AndGateAction(i, j, sum, this);
64 | andGate2.action();
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/java/adder/src/net/javajigi/adder/Wire.java:
--------------------------------------------------------------------------------
1 | package net.javajigi.adder;
2 |
3 | import java.util.Iterator;
4 | import java.util.concurrent.CopyOnWriteArrayList;
5 |
6 | public class Wire {
7 | private boolean sigVal = false;
8 | private CopyOnWriteArrayList actions = new CopyOnWriteArrayList();
9 |
10 | public boolean getSigVal() {
11 | return sigVal;
12 | }
13 |
14 | public void setSigVal(boolean newSigVal) {
15 | if( newSigVal != sigVal ) {
16 | sigVal = newSigVal;
17 | Iterator actIter = actions.iterator();
18 | while(actIter.hasNext()) {
19 | Action action = actIter.next();
20 | System.out.println("execute action : " + action.getClass().getCanonicalName());
21 | action.action();
22 | }
23 | }
24 | }
25 |
26 | public void addAction(Action action) {
27 | actions.add(action);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/java/adder/src/net/javajigi/adder/WireListener.java:
--------------------------------------------------------------------------------
1 | package net.javajigi.adder;
2 |
3 | public interface WireListener {
4 | void listen();
5 |
6 | int getDelayTime();
7 | }
8 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/java/adder/src/net/javajigi/adder/action/AndGateAction.java:
--------------------------------------------------------------------------------
1 | package net.javajigi.adder.action;
2 |
3 | import net.javajigi.adder.Action;
4 | import net.javajigi.adder.Simulation;
5 | import net.javajigi.adder.Wire;
6 | import net.javajigi.adder.WireListener;
7 |
8 | public class AndGateAction implements Action {
9 | private Wire a1;
10 | private Wire a2;
11 | private Wire output;
12 | private Simulation simulation;
13 |
14 | public AndGateAction(Wire a1, Wire a2, Wire output, Simulation simulation) {
15 | this.a1 = a1;
16 | this.a2 = a2;
17 | this.output = output;
18 | this.simulation = simulation;
19 | }
20 |
21 | @Override
22 | public void action() {
23 | simulation.addWireListener(new AndGateWireListener(a1, a2, output));
24 |
25 | a1.addAction(this);
26 | a2.addAction(this);
27 | }
28 |
29 | private class AndGateWireListener implements WireListener {
30 | private static final int DELAY_TIME = 3;
31 | private Wire a1;
32 | private Wire a2;
33 | private Wire output;
34 |
35 | public AndGateWireListener(Wire a1, Wire a2, Wire output) {
36 | this.a1 = a1;
37 | this.a2 = a2;
38 | this.output = output;
39 | }
40 |
41 | @Override
42 | public void listen() {
43 | boolean a1Sig = a1.getSigVal();
44 | boolean a2Sig = a2.getSigVal();
45 | System.out.println("a1Sig : " + a1Sig + " a2Sig : " + a2Sig);
46 | output.setSigVal(a1Sig & a2Sig);
47 | }
48 |
49 | @Override
50 | public int getDelayTime() {
51 | return DELAY_TIME;
52 | }
53 |
54 | };
55 | }
56 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/java/adder/src/net/javajigi/adder/action/InverterAction.java:
--------------------------------------------------------------------------------
1 | package net.javajigi.adder.action;
2 |
3 | import net.javajigi.adder.Action;
4 | import net.javajigi.adder.Simulation;
5 | import net.javajigi.adder.Wire;
6 | import net.javajigi.adder.WireListener;
7 |
8 | public class InverterAction implements Action {
9 | private Wire input;
10 | private Wire output;
11 | private Simulation simulation;
12 |
13 | public InverterAction(Wire input, Wire output, Simulation simulation) {
14 | this.input = input;
15 | this.output = output;
16 | this.simulation = simulation;
17 | }
18 |
19 | @Override
20 | public void action() {
21 | simulation.addWireListener(new InverterWireListener(input, output));
22 | input.addAction(this);
23 | }
24 |
25 | private class InverterWireListener implements WireListener{
26 | private static final int DELAY_TIME = 1;
27 | private Wire input;
28 | private Wire output;
29 |
30 | public InverterWireListener(Wire input, Wire output) {
31 | this.input = input;
32 | this.output = output;
33 | }
34 |
35 | @Override
36 | public void listen() {
37 | boolean inputSig = input.getSigVal();
38 | output.setSigVal(!inputSig);
39 | }
40 |
41 | @Override
42 | public int getDelayTime() {
43 | return DELAY_TIME;
44 | }
45 | };
46 | }
47 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/java/adder/src/net/javajigi/adder/action/OrGateAction.java:
--------------------------------------------------------------------------------
1 | package net.javajigi.adder.action;
2 |
3 | import net.javajigi.adder.Action;
4 | import net.javajigi.adder.Simulation;
5 | import net.javajigi.adder.Wire;
6 | import net.javajigi.adder.WireListener;
7 |
8 | public class OrGateAction implements Action {
9 | private Wire o1;
10 | private Wire o2;
11 | private Wire output;
12 | private Simulation simulation;
13 |
14 | public OrGateAction(Wire o1, Wire o2, Wire output, Simulation simulation) {
15 | this.o1 = o1;
16 | this.o2 = o2;
17 | this.output = output;
18 | this.simulation = simulation;
19 | }
20 |
21 | @Override
22 | public void action() {
23 | simulation.addWireListener(new OrGateWireListener(o1, o2, output));
24 |
25 | o1.addAction(this);
26 | o2.addAction(this);
27 | }
28 |
29 | private class OrGateWireListener implements WireListener {
30 | private static final int DELAY_TIME = 5;
31 | private Wire o1;
32 | private Wire o2;
33 | private Wire output;
34 |
35 | public OrGateWireListener(Wire o1, Wire o2, Wire output) {
36 | this.o1 = o1;
37 | this.o2 = o2;
38 | this.output = output;
39 | }
40 |
41 | @Override
42 | public void listen() {
43 | boolean o1Sig = o1.getSigVal();
44 | boolean o2Sig = o2.getSigVal();
45 | System.out.println("o1Sig : " + o1Sig + " o2Sig : " + o2Sig);
46 | output.setSigVal(o1Sig | o2Sig);
47 | }
48 |
49 | @Override
50 | public int getDelayTime() {
51 | return DELAY_TIME;
52 | }
53 | };
54 | }
55 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/java/adder/src/net/javajigi/adder/action/ProbeAction.java:
--------------------------------------------------------------------------------
1 | package net.javajigi.adder.action;
2 |
3 | import net.javajigi.adder.Action;
4 | import net.javajigi.adder.Simulation;
5 | import net.javajigi.adder.Wire;
6 |
7 | public class ProbeAction implements Action {
8 | private String name;
9 | private Wire wire;
10 | private Simulation simulation;
11 |
12 |
13 | public ProbeAction(String name, Wire wire, Simulation simulation) {
14 | this.name = name;
15 | this.wire = wire;
16 | this.simulation = simulation;
17 | }
18 |
19 | @Override
20 | public void action() {
21 | System.out.println(name + " " + simulation.getCurrentTime() + " new_value=" + wire.getSigVal());
22 |
23 | wire.addAction(this);
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/java/adder/test/net/javajigi/adder/CollectionTest.java:
--------------------------------------------------------------------------------
1 | package net.javajigi.adder;
2 |
3 | import java.util.Iterator;
4 | import java.util.concurrent.CopyOnWriteArrayList;
5 |
6 | import org.junit.Test;
7 |
8 |
9 | public class CollectionTest {
10 | @Test
11 | public void concurrent() throws Exception {
12 | CopyOnWriteArrayList list = new CopyOnWriteArrayList();
13 | list.add("vivek");
14 | list.add("kumar");
15 | Iterator i =list.iterator();
16 | while(i.hasNext()){
17 | System.out.println(i.next());
18 | list.add("abhishek");
19 | }
20 |
21 | System.out.println("After modification:");
22 | System.out.println("Size:" + list.size());
23 | Iterator i2 =list.iterator();
24 | while(i2.hasNext()){
25 | System.out.println(i2.next());
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/java/adder/test/net/javajigi/adder/MySimulationTest.java:
--------------------------------------------------------------------------------
1 | package net.javajigi.adder;
2 |
3 | import net.javajigi.adder.action.ProbeAction;
4 |
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | public class MySimulationTest {
9 | private Simulation simulation;
10 | private Wire input1;
11 | private Wire input2;
12 | private Wire sum;
13 | private Wire carry;
14 |
15 | @Before public void setup() {
16 | simulation = new Simulation();
17 | input1 = new Wire();
18 | input2 = new Wire();
19 | sum = new Wire();
20 | carry = new Wire();
21 | }
22 |
23 | @Test
24 | public void run() throws Exception {
25 | Action sumProbe = new ProbeAction("sum", sum, simulation);
26 | sumProbe.action();
27 |
28 | Action carryProbe = new ProbeAction("carry", carry, simulation);
29 | carryProbe.action();
30 |
31 | simulation.halfAdder(input1, input2, sum, carry);
32 | input1.setSigVal(true);
33 | simulation.run();
34 |
35 | input2.setSigVal(true);
36 | simulation.run();
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | net.javajigi
5 | scala-study
6 | 1.0-SNAPSHOT
7 | 2008
8 |
9 | 2.8.0
10 |
11 |
12 |
13 |
14 | scala-tools.org
15 | Scala-Tools Maven2 Repository
16 | http://scala-tools.org/repo-releases
17 |
18 |
19 |
20 |
21 |
22 | scala-tools.org
23 | Scala-Tools Maven2 Repository
24 | http://scala-tools.org/repo-releases
25 |
26 |
27 |
28 |
29 |
30 | org.scala-lang
31 | scala-library
32 | ${scala.version}
33 |
34 |
35 | junit
36 | junit
37 | 4.8
38 | test
39 |
40 |
41 | org.scalatest
42 | scalatest
43 | 1.2
44 | test
45 |
46 |
47 | org.hsqldb
48 | hsqldb
49 | 2.0.0
50 |
51 |
52 |
53 |
54 | src/main/scala
55 | src/test/scala
56 |
57 |
58 | org.scala-tools
59 | maven-scala-plugin
60 |
61 |
62 |
63 | compile
64 | testCompile
65 |
66 |
67 |
68 |
69 | ${scala.version}
70 |
71 |
72 |
73 | org.apache.maven.plugins
74 | maven-eclipse-plugin
75 |
76 | true
77 |
78 | ch.epfl.lamp.sdt.core.scalabuilder
79 |
80 |
81 | ch.epfl.lamp.sdt.core.scalanature
82 |
83 |
84 | org.eclipse.jdt.launching.JRE_CONTAINER
85 | ch.epfl.lamp.sdt.launching.SCALA_CONTAINER
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 | org.scala-tools
95 | maven-scala-plugin
96 |
97 | ${scala.version}
98 |
99 |
100 |
101 |
102 |
103 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/.svn/entries:
--------------------------------------------------------------------------------
1 | 9
2 |
3 | dir
4 | 522
5 | svn://www.javajigi.net/repo/scala-study/src
6 | svn://www.javajigi.net
7 |
8 |
9 |
10 | 2010-12-08T13:06:46.219010Z
11 | 522
12 | javajigi
13 |
14 |
15 | svn:special svn:externals svn:needs-lock
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | 161d8129-efa4-44e7-b7d7-98ae61b7b6e9
28 |
29 |
30 |
31 |
32 |
33 |
34 | 0
35 |
36 | main
37 | dir
38 |
39 | test
40 | dir
41 |
42 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/.svn/format:
--------------------------------------------------------------------------------
1 | 9
2 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/.svn/entries:
--------------------------------------------------------------------------------
1 | 9
2 |
3 | dir
4 | 522
5 | svn://www.javajigi.net/repo/scala-study/src/main
6 | svn://www.javajigi.net
7 |
8 |
9 |
10 | 2010-12-08T13:06:46.219010Z
11 | 522
12 | javajigi
13 |
14 |
15 | svn:special svn:externals svn:needs-lock
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | 161d8129-efa4-44e7-b7d7-98ae61b7b6e9
28 |
29 |
30 |
31 |
32 |
33 |
34 | 0
35 |
36 | scala
37 | dir
38 |
39 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/.svn/format:
--------------------------------------------------------------------------------
1 | 9
2 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/.svn/entries:
--------------------------------------------------------------------------------
1 | 9
2 |
3 | dir
4 | 522
5 | svn://www.javajigi.net/repo/scala-study/src/main/scala
6 | svn://www.javajigi.net
7 |
8 |
9 |
10 | 2010-12-08T13:06:46.219010Z
11 | 522
12 | javajigi
13 |
14 |
15 | svn:special svn:externals svn:needs-lock
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | 161d8129-efa4-44e7-b7d7-98ae61b7b6e9
28 |
29 |
30 |
31 |
32 |
33 |
34 | 0
35 |
36 | net
37 | dir
38 |
39 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/.svn/format:
--------------------------------------------------------------------------------
1 | 9
2 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/.svn/entries:
--------------------------------------------------------------------------------
1 | 9
2 |
3 | dir
4 | 522
5 | svn://www.javajigi.net/repo/scala-study/src/main/scala/net
6 | svn://www.javajigi.net
7 |
8 |
9 |
10 | 2010-12-08T13:06:46.219010Z
11 | 522
12 | javajigi
13 |
14 |
15 | svn:special svn:externals svn:needs-lock
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | 161d8129-efa4-44e7-b7d7-98ae61b7b6e9
28 |
29 |
30 |
31 |
32 |
33 |
34 | 0
35 |
36 | javajigi
37 | dir
38 |
39 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/.svn/format:
--------------------------------------------------------------------------------
1 | 9
2 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/javajigi/.svn/entries:
--------------------------------------------------------------------------------
1 | 9
2 |
3 | dir
4 | 522
5 | svn://www.javajigi.net/repo/scala-study/src/main/scala/net/javajigi
6 | svn://www.javajigi.net
7 |
8 |
9 |
10 | 2010-12-08T13:06:46.219010Z
11 | 522
12 | javajigi
13 |
14 |
15 | svn:special svn:externals svn:needs-lock
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | 161d8129-efa4-44e7-b7d7-98ae61b7b6e9
28 |
29 |
30 |
31 |
32 |
33 |
34 | 0
35 |
36 | chapter10
37 | dir
38 |
39 | chapter11
40 | dir
41 |
42 | chapter7
43 | dir
44 |
45 | chapter8
46 | dir
47 |
48 | chapter9
49 | dir
50 |
51 | user
52 | dir
53 |
54 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/javajigi/.svn/format:
--------------------------------------------------------------------------------
1 | 9
2 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/javajigi/chapter10/.svn/entries:
--------------------------------------------------------------------------------
1 | 9
2 |
3 | dir
4 | 522
5 | svn://www.javajigi.net/repo/scala-study/src/main/scala/net/javajigi/chapter10
6 | svn://www.javajigi.net
7 |
8 |
9 |
10 | 2010-11-26T08:37:21.200904Z
11 | 520
12 | javajigi
13 |
14 |
15 | svn:special svn:externals svn:needs-lock
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | 161d8129-efa4-44e7-b7d7-98ae61b7b6e9
28 |
29 | Person.scala
30 | file
31 |
32 |
33 |
34 |
35 | 2010-06-08T04:11:36.000000Z
36 | 597ccaf216e5c6d540dd77a8882fd877
37 | 2010-11-26T08:37:21.200904Z
38 | 520
39 | javajigi
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 | 77
62 |
63 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/javajigi/chapter10/.svn/format:
--------------------------------------------------------------------------------
1 | 9
2 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/javajigi/chapter10/.svn/text-base/Person.scala.svn-base:
--------------------------------------------------------------------------------
1 | package net.javajigi.chapter10
2 |
3 | class Person(val age:Int, val name:String )
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/javajigi/chapter10/Person.scala:
--------------------------------------------------------------------------------
1 | package net.javajigi.chapter10
2 |
3 | class Person(val age:Int, val name:String )
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/javajigi/chapter11/.svn/entries:
--------------------------------------------------------------------------------
1 | 9
2 |
3 | dir
4 | 522
5 | svn://www.javajigi.net/repo/scala-study/src/main/scala/net/javajigi/chapter11
6 | svn://www.javajigi.net
7 |
8 |
9 |
10 | 2010-12-08T13:06:46.219010Z
11 | 522
12 | javajigi
13 |
14 |
15 | svn:special svn:externals svn:needs-lock
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | 161d8129-efa4-44e7-b7d7-98ae61b7b6e9
28 |
29 |
30 |
31 |
32 |
33 |
34 | 0
35 |
36 | BasicCircuitSimulation.scala
37 | file
38 |
39 |
40 |
41 |
42 | 2010-06-19T13:59:08.000000Z
43 | 3167e99af46a5c024669a3d956e3f95d
44 | 2010-12-08T13:06:46.219010Z
45 | 522
46 | javajigi
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 | 1458
69 |
70 | CircuitSimulation.scala
71 | file
72 |
73 |
74 |
75 |
76 | 2010-06-19T13:39:08.000000Z
77 | 4085c1f44c74cdcdcdbc8e28c3b54dd4
78 | 2010-12-08T12:30:13.232539Z
79 | 521
80 | javajigi
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 | 492
103 |
104 | MySimulation.scala
105 | file
106 |
107 |
108 |
109 |
110 | 2010-06-19T13:41:35.000000Z
111 | 38ff10086deb586c4c2f82bd9a354606
112 | 2010-12-08T12:30:13.232539Z
113 | 521
114 | javajigi
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 | 150
137 |
138 | Simulation.scala
139 | file
140 |
141 |
142 |
143 |
144 | 2010-06-19T14:17:32.000000Z
145 | f7b7686f421b69dcf1596212fe7840b8
146 | 2010-12-08T13:06:46.219010Z
147 | 522
148 | javajigi
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 | 889
171 |
172 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/javajigi/chapter11/.svn/format:
--------------------------------------------------------------------------------
1 | 9
2 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/javajigi/chapter11/.svn/text-base/BasicCircuitSimulation.scala.svn-base:
--------------------------------------------------------------------------------
1 | package net.javajigi.chapter11
2 |
3 | abstract class BasicCircuitSimulation extends Simulation {
4 | def InverterDelay: Int
5 | def AndGateDelay: Int
6 | def OrGateDelay: Int
7 |
8 | class Wire {
9 | private var sigVal = false
10 | private var actions: List[Action] = List()
11 |
12 | def getSignal = sigVal
13 |
14 | def setSignal(s: Boolean) =
15 | if (s != sigVal) {
16 | sigVal = s
17 | actions foreach (_ ())
18 | }
19 |
20 | def addAction(a: Action) = {
21 | actions = a :: actions
22 | a()
23 | }
24 | }
25 |
26 | def inverter(input: Wire, output: Wire) = {
27 | def invertAction() {
28 | val inputSig = input.getSignal
29 | afterDelay(InverterDelay) {
30 | output setSignal !inputSig
31 | }
32 | }
33 | input addAction invertAction
34 | }
35 |
36 | def andGate(a1: Wire, a2: Wire, output: Wire) = {
37 | def andAction() = {
38 | val a1Sig = a1.getSignal
39 | val a2Sig = a2.getSignal
40 | afterDelay(AndGateDelay) {
41 | output setSignal (a1Sig & a2Sig)
42 | }
43 | }
44 | a1 addAction andAction
45 | a2 addAction andAction
46 | }
47 |
48 | def orGate(o1: Wire, o2: Wire, output: Wire) {
49 | def orAction() {
50 | val o1Sig = o1.getSignal
51 | val o2Sig = o2.getSignal
52 | afterDelay(OrGateDelay) {
53 | output setSignal (o1Sig | o2Sig)
54 | }
55 | }
56 | o1 addAction orAction
57 | o2 addAction orAction
58 | }
59 |
60 | def probe(name: String, wire: Wire) {
61 | def probeAction() {
62 | println(name +" "+ currentTime + " new-value = "+ wire.getSignal)
63 | }
64 |
65 | wire addAction probeAction
66 | }
67 | }
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/javajigi/chapter11/.svn/text-base/CircuitSimulation.scala.svn-base:
--------------------------------------------------------------------------------
1 | package net.javajigi.chapter11
2 |
3 | abstract class CircuitSimulation
4 | extends BasicCircuitSimulation {
5 |
6 | def halfAdder(a: Wire, b: Wire, s: Wire, c: Wire) {
7 | val d, e = new Wire
8 | orGate(a, b, d)
9 | andGate(a, b, c)
10 | inverter(c, e)
11 | andGate(d, e, s)
12 | }
13 |
14 | def fullAdder(a: Wire, b: Wire, cin: Wire,
15 | sum: Wire, cout: Wire) {
16 |
17 | val s, c1, c2 = new Wire
18 | halfAdder(a, cin, s, c1)
19 | halfAdder(b, s, sum, c2)
20 | orGate(c1, c2, cout)
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/javajigi/chapter11/.svn/text-base/MySimulation.scala.svn-base:
--------------------------------------------------------------------------------
1 | package net.javajigi.chapter11
2 |
3 | object MySimulation extends CircuitSimulation {
4 | def InverterDelay = 1
5 | def AndGateDelay = 3
6 | def OrGateDelay = 5
7 | }
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/javajigi/chapter11/.svn/text-base/Simulation.scala.svn-base:
--------------------------------------------------------------------------------
1 | package net.javajigi.chapter11
2 |
3 | abstract class Simulation {
4 |
5 | type Action = () => Unit
6 |
7 | case class WorkItem(time: Int, action: Action)
8 |
9 | private var curtime = 0
10 | def currentTime: Int = curtime
11 |
12 | private var agenda: List[WorkItem] = List()
13 |
14 | private def insert(ag: List[WorkItem],item: WorkItem): List[WorkItem] = {
15 | if (ag.isEmpty || item.time < ag.head.time) item :: ag
16 | else ag.head :: insert(ag.tail, item)
17 | }
18 |
19 | def afterDelay(delay: Int)(block: => Unit) {
20 | val item = WorkItem(currentTime + delay, () => block)
21 | agenda = insert(agenda, item)
22 | }
23 |
24 | private def next() {
25 | agenda match {
26 | case WorkItem(time, action) :: rest =>
27 | agenda = rest; curtime = time; action()
28 | case List() =>
29 | }
30 | }
31 |
32 | def run() {
33 | afterDelay(0) {
34 | println("*** simulation started, time = "+ currentTime +" ***")
35 | }
36 | while (!agenda.isEmpty) next()
37 | }
38 | }
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/javajigi/chapter11/BasicCircuitSimulation.scala:
--------------------------------------------------------------------------------
1 | package net.javajigi.chapter11
2 |
3 | abstract class BasicCircuitSimulation extends Simulation {
4 | def InverterDelay: Int
5 | def AndGateDelay: Int
6 | def OrGateDelay: Int
7 |
8 | class Wire {
9 | private var sigVal = false
10 | private var actions: List[Action] = List()
11 |
12 | def getSignal = sigVal
13 |
14 | def setSignal(s: Boolean) =
15 | if (s != sigVal) {
16 | sigVal = s
17 | actions foreach (action => action())
18 | }
19 |
20 | def addAction(a: Action) = {
21 | actions = a :: actions
22 | println("triggering added!!")
23 | a()
24 | }
25 | }
26 |
27 | def inverter(input: Wire, output: Wire) = {
28 | def invertAction() {
29 | val inputSig = input.getSignal
30 | println("inverter : inputSig : " + inputSig)
31 | afterDelay(InverterDelay) {
32 | println("triggering inverter afterDelay")
33 | output setSignal !inputSig
34 | }
35 | }
36 | input addAction invertAction
37 | }
38 |
39 | def andGate(a1: Wire, a2: Wire, output: Wire) = {
40 | def andAction() = {
41 | val a1Sig = a1.getSignal
42 | val a2Sig = a2.getSignal
43 | println("andAction : a1Sig : " + a1Sig + " a2Sig : " + a2Sig)
44 | afterDelay(AndGateDelay) {
45 | println("triggering andGate afterDelay")
46 | output setSignal (a1Sig & a2Sig)
47 | }
48 | }
49 | a1 addAction andAction
50 | a2 addAction andAction
51 | }
52 |
53 | def orGate(o1: Wire, o2: Wire, output: Wire) {
54 | def orAction() {
55 | val o1Sig = o1.getSignal
56 | val o2Sig = o2.getSignal
57 | println("orAction: o1Sig : " + o1Sig + " o2Sig : " + o2Sig)
58 | afterDelay(OrGateDelay) {
59 | println("triggering orGate afterDelay")
60 | output setSignal (o1Sig | o2Sig)
61 | }
62 | }
63 | o1 addAction orAction
64 | o2 addAction orAction
65 | }
66 |
67 | def probe(name: String, wire: Wire) {
68 | def probeAction() {
69 | println(name +" "+ currentTime + " new-value = "+ wire.getSignal)
70 | }
71 |
72 | wire addAction probeAction
73 | }
74 | }
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/javajigi/chapter11/CircuitSimulation.scala:
--------------------------------------------------------------------------------
1 | package net.javajigi.chapter11
2 |
3 | abstract class CircuitSimulation
4 | extends BasicCircuitSimulation {
5 |
6 | def halfAdder(a: Wire, b: Wire, s: Wire, c: Wire) {
7 | val d, e = new Wire
8 | orGate(a, b, d)
9 | andGate(a, b, c)
10 | inverter(c, e)
11 | andGate(d, e, s)
12 | }
13 |
14 | def fullAdder(a: Wire, b: Wire, cin: Wire,
15 | sum: Wire, cout: Wire) {
16 |
17 | val s, c1, c2 = new Wire
18 | halfAdder(a, cin, s, c1)
19 | halfAdder(b, s, sum, c2)
20 | orGate(c1, c2, cout)
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/javajigi/chapter11/MySimulation.scala:
--------------------------------------------------------------------------------
1 | package net.javajigi.chapter11
2 |
3 | object MySimulation extends CircuitSimulation {
4 | def InverterDelay = 1
5 | def AndGateDelay = 3
6 | def OrGateDelay = 5
7 | }
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/main/scala/net/javajigi/chapter11/Simulation.scala:
--------------------------------------------------------------------------------
1 | package net.javajigi.chapter11
2 |
3 | abstract class Simulation {
4 |
5 | type Action = () => Unit
6 |
7 | case class WorkItem(time: Int, action: Action)
8 |
9 | private var curtime = 0
10 | def currentTime: Int = curtime
11 |
12 | private var agenda: List[WorkItem] = List()
13 |
14 | private def insert(ag: List[WorkItem],item: WorkItem): List[WorkItem] = {
15 | if (ag.isEmpty || item.time < ag.head.time) item :: ag
16 | else ag.head :: insert(ag.tail, item)
17 | }
18 |
19 | def afterDelay(delay: Int)(block: => Unit) {
20 | println("afterDealy : delay time : " + delay)
21 | val item = WorkItem(currentTime + delay, () => block)
22 | agenda = insert(agenda, item)
23 | }
24 |
25 | private def next() {
26 | agenda match {
27 | case WorkItem(time, action) :: rest =>
28 | agenda = rest; curtime = time; action()
29 | case List() =>
30 | }
31 | }
32 |
33 | def run() {
34 | afterDelay(0) {
35 | println("*** simulation started, time = "+ currentTime +" ***")
36 | }
37 | while (!agenda.isEmpty) next()
38 | }
39 | }
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/test/.svn/entries:
--------------------------------------------------------------------------------
1 | 9
2 |
3 | dir
4 | 522
5 | svn://www.javajigi.net/repo/scala-study/src/test
6 | svn://www.javajigi.net
7 |
8 |
9 |
10 | 2010-12-08T12:30:13.232539Z
11 | 521
12 | javajigi
13 |
14 |
15 | svn:special svn:externals svn:needs-lock
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | 161d8129-efa4-44e7-b7d7-98ae61b7b6e9
28 |
29 |
30 |
31 |
32 |
33 |
34 | 0
35 |
36 | scala
37 | dir
38 |
39 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/test/.svn/format:
--------------------------------------------------------------------------------
1 | 9
2 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/test/scala/.svn/entries:
--------------------------------------------------------------------------------
1 | 9
2 |
3 | dir
4 | 522
5 | svn://www.javajigi.net/repo/scala-study/src/test/scala
6 | svn://www.javajigi.net
7 |
8 |
9 |
10 | 2010-12-08T12:30:13.232539Z
11 | 521
12 | javajigi
13 |
14 |
15 | svn:special svn:externals svn:needs-lock
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | 161d8129-efa4-44e7-b7d7-98ae61b7b6e9
28 |
29 |
30 |
31 |
32 |
33 |
34 | 0
35 |
36 | net
37 | dir
38 |
39 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/test/scala/.svn/format:
--------------------------------------------------------------------------------
1 | 9
2 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/test/scala/net/.svn/entries:
--------------------------------------------------------------------------------
1 | 9
2 |
3 | dir
4 | 522
5 | svn://www.javajigi.net/repo/scala-study/src/test/scala/net
6 | svn://www.javajigi.net
7 |
8 |
9 |
10 | 2010-12-08T12:30:13.232539Z
11 | 521
12 | javajigi
13 |
14 |
15 | svn:special svn:externals svn:needs-lock
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | 161d8129-efa4-44e7-b7d7-98ae61b7b6e9
28 |
29 |
30 |
31 |
32 |
33 |
34 | 0
35 |
36 | javajigi
37 | dir
38 |
39 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/test/scala/net/.svn/format:
--------------------------------------------------------------------------------
1 | 9
2 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/test/scala/net/javajigi/.svn/entries:
--------------------------------------------------------------------------------
1 | 9
2 |
3 | dir
4 | 522
5 | svn://www.javajigi.net/repo/scala-study/src/test/scala/net/javajigi
6 | svn://www.javajigi.net
7 |
8 |
9 |
10 | 2010-12-08T12:30:13.232539Z
11 | 521
12 | javajigi
13 |
14 |
15 | svn:special svn:externals svn:needs-lock
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | 161d8129-efa4-44e7-b7d7-98ae61b7b6e9
28 |
29 |
30 |
31 |
32 |
33 |
34 | 0
35 |
36 | chapter10
37 | dir
38 |
39 | chapter11
40 | dir
41 |
42 | chapter7
43 | dir
44 |
45 | chapter8
46 | dir
47 |
48 | chapter9
49 | dir
50 |
51 | user
52 | dir
53 |
54 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/test/scala/net/javajigi/.svn/format:
--------------------------------------------------------------------------------
1 | 9
2 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/test/scala/net/javajigi/chapter10/.svn/entries:
--------------------------------------------------------------------------------
1 | 9
2 |
3 | dir
4 | 522
5 | svn://www.javajigi.net/repo/scala-study/src/test/scala/net/javajigi/chapter10
6 | svn://www.javajigi.net
7 |
8 |
9 |
10 | 2010-11-26T08:37:21.200904Z
11 | 520
12 | javajigi
13 |
14 |
15 | svn:special svn:externals svn:needs-lock
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | 161d8129-efa4-44e7-b7d7-98ae61b7b6e9
28 |
29 | ForComprehensionsTest.scala
30 | file
31 |
32 |
33 |
34 |
35 | 2010-06-08T04:11:36.000000Z
36 | a3ea804c8bf080f594759c1c2e21c776
37 | 2010-11-26T08:37:21.200904Z
38 | 520
39 | javajigi
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 | 553
62 |
63 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/test/scala/net/javajigi/chapter10/.svn/format:
--------------------------------------------------------------------------------
1 | 9
2 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/test/scala/net/javajigi/chapter10/.svn/text-base/ForComprehensionsTest.scala.svn-base:
--------------------------------------------------------------------------------
1 | package net.javajigi.chapter10
2 |
3 | import org.junit.Test
4 | import org.junit.Assert._
5 |
6 | import org.scalatest.junit.AssertionsForJUnit
7 |
8 | class ForComprehensionsTest extends AssertionsForJUnit {
9 | @Test def for_yield() {
10 | val persons = new Person(38, "재성") :: new Person(38, "영정") :: new Person(10, "예은") :: new Person(8, "주한") :: Nil
11 | assertEquals("재성" :: "영정" :: Nil , persons filter(p => p.age > 15) map (p => p.name))
12 | assertEquals("재성" :: "영정" :: Nil , for(p <- persons if p.age > 15) yield p.name)
13 | }
14 | }
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/test/scala/net/javajigi/chapter10/ForComprehensionsTest.scala:
--------------------------------------------------------------------------------
1 | package net.javajigi.chapter10
2 |
3 | import org.junit.Test
4 | import org.junit.Assert._
5 |
6 | import org.scalatest.junit.AssertionsForJUnit
7 |
8 | class ForComprehensionsTest extends AssertionsForJUnit {
9 | @Test def for_yield() {
10 | val persons = new Person(38, "재성") :: new Person(38, "영정") :: new Person(10, "예은") :: new Person(8, "주한") :: Nil
11 | assertEquals("재성" :: "영정" :: Nil , persons filter(p => p.age > 15) map (p => p.name))
12 | assertEquals("재성" :: "영정" :: Nil , for(p <- persons if p.age > 15) yield p.name)
13 | }
14 | }
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/test/scala/net/javajigi/chapter11/.svn/entries:
--------------------------------------------------------------------------------
1 | 9
2 |
3 | dir
4 | 522
5 | svn://www.javajigi.net/repo/scala-study/src/test/scala/net/javajigi/chapter11
6 | svn://www.javajigi.net
7 |
8 |
9 |
10 | 2010-12-08T12:30:13.232539Z
11 | 521
12 | javajigi
13 |
14 |
15 | svn:special svn:externals svn:needs-lock
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | 161d8129-efa4-44e7-b7d7-98ae61b7b6e9
28 |
29 |
30 |
31 |
32 |
33 |
34 | 0
35 |
36 | MySimulationTest.scala
37 | file
38 |
39 |
40 |
41 |
42 | 2010-06-19T13:48:45.000000Z
43 | 8f63f850928423d02f8b25943b550cb3
44 | 2010-12-08T12:30:13.232539Z
45 | 521
46 | javajigi
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 | 447
69 |
70 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/test/scala/net/javajigi/chapter11/.svn/format:
--------------------------------------------------------------------------------
1 | 9
2 |
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/test/scala/net/javajigi/chapter11/.svn/text-base/MySimulationTest.scala.svn-base:
--------------------------------------------------------------------------------
1 | package net.javajigi.chapter11
2 |
3 | import net.javajigi.chapter11.MySimulation._
4 |
5 | import org.scalatest.junit.AssertionsForJUnit
6 | import org.junit.{Before, Test}
7 |
8 | class MySimulationTest extends AssertionsForJUnit {
9 |
10 | @Test def runner() {
11 | val input1, input2, sum, carry = new Wire
12 | probe("sum", sum)
13 | probe("carry", carry)
14 | halfAdder(input1, input2, sum, carry)
15 | input1 setSignal true
16 | run()
17 |
18 | input2 setSignal true
19 | run()
20 | }
21 | }
--------------------------------------------------------------------------------
/sbestudy/javajigi/scala/src/test/scala/net/javajigi/chapter11/MySimulationTest.scala:
--------------------------------------------------------------------------------
1 | package net.javajigi.chapter11
2 |
3 | import net.javajigi.chapter11.MySimulation._
4 |
5 | import org.scalatest.junit.AssertionsForJUnit
6 | import org.junit.{Before, Test}
7 |
8 | class MySimulationTest extends AssertionsForJUnit {
9 |
10 | @Test def runner() {
11 | val input1, input2, sum, carry = new Wire
12 | probe("sum", sum)
13 | probe("carry", carry)
14 | halfAdder(input1, input2, sum, carry)
15 | run()
16 | input1 setSignal true
17 | run()
18 |
19 | input2 setSignal true
20 | run()
21 | }
22 | }
--------------------------------------------------------------------------------
/sbestudy/nephilim/chap16/.HindleyMilner.scala.swp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codeport/scala/e5e560f8bf12f35d50cb77b55bc587e4f8091473/sbestudy/nephilim/chap16/.HindleyMilner.scala.swp
--------------------------------------------------------------------------------
/sbestudy/nephilim/chap16/HindleyMilner.scala:
--------------------------------------------------------------------------------
1 | package chap16
2 |
3 | abstract class Term {}
4 |
5 | case class Var(x:String) extends Term {
6 | override def toString = x;
7 | }
8 |
9 | case class Lam(x:String, e:Term) extends Term {
10 | override def toString = "(\\" + x + "." + e + ")"
11 | }
12 |
13 | case class App(f:Term, e:Term) extends Term {
14 | override def toString = "(" + f + " " + e + ")"
15 | }
16 |
17 | case class Let(x:String, e:Term, f:Term) extends Term {
18 | override def toString = "let " + x + " = " + e + " in " + f
19 | }
20 |
21 | sealed abstract class Type {}
22 | case class Tyvar(a:String) extends Type {
23 | override def toString = a
24 | }
25 |
26 | case class Arrow(t1:Type, t2:Type) extends Type {
27 | override def toString = "(" + t1 + "->" + t2 + ")"
28 | }
29 |
30 | case class Tycon(k:String, ts:List[Type]) extends Type {
31 | override def toString = k + (if (ts.isEmpty) "" else ts.mkString("[",",","]"))
32 | }
33 |
34 | object typeInfer {
35 | private var n:Int = 0
36 | def newTyvar():Type = { n += 1; Tyvar("a" + n) }
37 |
38 | abstract class Subst extends Function1[Type, Type] {
39 | def lookup(x:Tyvar):Type
40 |
41 | def apply(t:Type):Type = t match {
42 | case tv@Tyvar(a) => val u = lookup(tv); if ( u == t ) t else apply(u)
43 | case Arrow(t1,t2) => Arrow(apply(t1), apply(t2))
44 | case Tycon(k, ts) => Tycon(k, ts map apply)
45 | }
46 |
47 | def extend(x:Tyvar, t:Type) = new Subst {
48 | def lookup( y: Tyvar ):Type = if ( x==y ) t else Subst.this.lookup(y)
49 | }
50 | }
51 |
52 | val emptySubst = new Subst { def lookup(t:Tyvar):Type = t }
53 |
54 | case class TypeScheme(tyvars:List[Tyvar], tpe:Type) {
55 | def newInstance: Type = {
56 | (emptySubst /: tyvars) ((s,tv) => s.extend(tv, typeInfer.newTyvar())) (tpe)
57 | }
58 | }
59 |
60 | type Env = List[ (String, TypeScheme) ]
61 | def lookup(env: Env, x:String): TypeScheme = env match {
62 | case List() => null
63 | case (y,t) :: env1 => if ( x== y) t else lookup(env1, x)
64 | }
65 |
66 | def tyvars(t:Type):List[Tyvar] = t match {
67 | case tv@Tyvar(a) => List(tv)
68 | case Arrow(t1,t2) => tyvars(t1) union tyvars(t2)
69 | case Tycon(k,ts) => (List[Tyvar]() /: ts) ((tvs, t) => tvs union tyvars(t))
70 | }
71 |
72 | //scala> tyvars(Tyvar("a"))
73 | //res6: List[Tyvar] = List(a)
74 | //scala> tyvars(Arrow(Tyvar("a"),Tycon("b",List(Tyvar("c"),Tyvar("d")))) )
75 | //res11: List[Tyvar] = List(a, c, d)
76 |
77 | def tyvars(ts:TypeScheme): List[Tyvar] =
78 | tyvars(ts.tpe) diff ts.tyvars
79 |
80 | def tyvars(env:Env):List[Tyvar] =
81 | (List[Tyvar]() /: env) ( (tvs, nt) => tvs union tyvars(nt._2))
82 |
83 | def gen( env:Env, t:Type): TypeScheme =
84 | TypeScheme(tyvars(t) diff tyvars(env), t)
85 |
86 | case class TypeError(s: String) extends Exception(s) {}
87 |
88 | def mgu(t: Type, u: Type, s: Subst): Subst = (s(t), s(u)) match {
89 | case (Tyvar(a), Tyvar(b)) if (a == b) => s
90 | case (Tyvar(a), _) if !(tyvars(u) contains a) => s.extend(Tyvar(a), u)
91 | case (_, Tyvar(a)) => mgu(u, t, s)
92 | case (Arrow(t1, t2), Arrow(u1, u2)) => mgu(t1, u1, mgu(t2, u2, s))
93 | case (Tycon(k1, ts), Tycon(k2, us)) if (k1 == k2) =>
94 | (s /: (ts zip us)) ((s, tu) => mgu(tu._1, tu._2, s))
95 | case _ => throw new TypeError("cannot unify " + s(t) + " with " + s(u))
96 | }
97 |
98 | def tp(env: Env, e: Term, t: Type, s: Subst): Subst = {
99 | current = e
100 | e match {
101 | case Var(x) => {
102 | val u = lookup(env, x)
103 | if (u == null) throw new TypeError("undefined: " + x)
104 | else mgu(u.newInstance, t, s)
105 | }
106 | case Lam(x, e1) => {
107 | val a, b = newTyvar()
108 | val s1 = mgu(t, Arrow(a, b), s)
109 | val env1 = (x, TypeScheme(List(), a)) :: env
110 | tp(env1, e1, b, s1)
111 | }
112 | case App(e1, e2) =>
113 | {
114 | val a = newTyvar()
115 | val s1 = tp(env, e1, Arrow(a, t), s)
116 | tp(env, e2, a, s1)
117 | }
118 | case Let(x, e1, e2) =>
119 | {
120 | val a = newTyvar()
121 | val s1 = tp(env, e1, a, s)
122 | tp((x, gen(env, s1(a))) :: env, e2, t, s1)
123 | }
124 | }}
125 | var current: Term = null
126 | def typeOf(env: Env, e: Term): Type = {
127 | val a = newTyvar()
128 | tp(env, e, a, emptySubst)(a)
129 | }
130 | }
131 |
132 |
133 | object predefined {
134 | val booleanType = Tycon("Boolean", List())
135 | val intType = Tycon("Int", List())
136 | def listType(t: Type) = Tycon("List", List(t))
137 | private def gen(t: Type): typeInfer.TypeScheme = typeInfer.gen(List(), t)
138 | private val a = typeInfer.newTyvar()
139 | val env = List(
140 | ("true", gen(booleanType)),
141 | ("false", gen(booleanType)),
142 | ("if", gen(Arrow(booleanType, Arrow(a, Arrow(a, a))))),
143 | ("zero", gen(intType)), ("succ", gen(Arrow(intType, intType))),
144 | ("nil", gen(listType(a))),
145 | ("cons", gen(Arrow(a, Arrow(listType(a), listType(a))))),
146 | ("isEmpty", gen(Arrow(listType(a), booleanType))),
147 | ("head", gen(Arrow(listType(a), a))),
148 | ("tail", gen(Arrow(listType(a), listType(a)))),
149 | ("fix", gen(Arrow(Arrow(a, a), a)))
150 | )
151 | }
152 |
153 | object testInfer {
154 | def showType(e: Term): String =
155 | try {
156 | typeInfer.typeOf(predefined.env, e).toString
157 | } catch {
158 | case typeInfer.TypeError(msg) =>
159 | "\n cannot type: " + typeInfer.current +
160 | "\n reason: " + msg
161 | }
162 | }
163 |
164 |
165 |
166 |
--------------------------------------------------------------------------------
/sbestudy/outsider/chapter10/project/build.properties:
--------------------------------------------------------------------------------
1 | #Project properties
2 | #Thu Dec 23 01:42:54 KST 2010
3 | project.organization=La Scala Coding Dan
4 | project.name=SBE chapter 10
5 | sbt.version=0.7.4
6 | project.version=0.1
7 | build.scala.versions=2.8.1
8 | project.initialize=false
9 |
--------------------------------------------------------------------------------
/sbestudy/outsider/chapter10/project/build/ManageDependencies.scala:
--------------------------------------------------------------------------------
1 | package sbt
2 |
3 | class ManageDependencies(info: ProjectInfo) extends DefaultProject(info) {
4 | val junitInterface = "com.novocode" % "junit-interface" % "0.5" % "test->default"
5 | }
6 |
--------------------------------------------------------------------------------
/scalacheck/project/build.properties:
--------------------------------------------------------------------------------
1 | #Project properties
2 | #Sat Feb 26 23:54:19 KST 2011
3 | project.organization=ysl.nephilim.test
4 | project.name=ScalaCheck
5 | sbt.version=0.7.4
6 | project.version=1.0
7 | build.scala.versions=2.8.1
8 | project.initialize=false
9 |
--------------------------------------------------------------------------------
/scalacheck/project/build/Project.scala:
--------------------------------------------------------------------------------
1 | import sbt._
2 |
3 | class MyProject(info:ProjectInfo) extends DefaultProject(info) {
4 | val scalaToolsReleases = ScalaToolsReleases // http://scala-tools.org/repo-release/
5 | val scalacheck = "org.scalacheck" % "scalacheck" % "1.5"
6 |
7 | }
8 |
9 |
--------------------------------------------------------------------------------