├── .babelrc ├── .envrc ├── .gitignore ├── default.nix ├── package.json ├── pom.xml ├── src ├── MyMovieCollectionSpecs.hs ├── main │ └── java │ │ └── fp │ │ ├── Movie.java │ │ └── MovieFinder.java └── test │ └── java │ └── fp │ └── MovieFinderTest.java └── test ├── mocha.opts └── myMovieCollectionSpecs.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015", "stage-2"], 3 | "plugins": [] 4 | } -------------------------------------------------------------------------------- /.envrc: -------------------------------------------------------------------------------- 1 | use_nix -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Node template 3 | # Logs 4 | logs 5 | *.log 6 | npm-debug.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # node-waf configuration 27 | .lock-wscript 28 | 29 | # Compiled binary addons (http://nodejs.org/api/addons.html) 30 | build/Release 31 | 32 | # Dependency directories 33 | node_modules 34 | jspm_packages 35 | 36 | # Optional npm cache directory 37 | .npm 38 | 39 | # Optional eslint cache 40 | .eslintcache 41 | 42 | # Optional REPL history 43 | .node_repl_history 44 | 45 | # Output of 'npm pack' 46 | *.tgz 47 | 48 | # Yarn Integrity file 49 | .yarn-integrity 50 | 51 | .idea 52 | -------------------------------------------------------------------------------- /default.nix: -------------------------------------------------------------------------------- 1 | with import {}; { 2 | nodeEnv = stdenv.mkDerivation { 3 | name = "javascript-sandbox"; 4 | buildInputs = [ 5 | pkgs.nodejs 6 | pkgs.ghc 7 | ]; 8 | }; 9 | } 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "js-transducers", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "transducers.js", 6 | "dependencies": {}, 7 | "devDependencies": { 8 | "babel-cli": "^6.18.0", 9 | "babel-preset-es2015": "^6.18.0", 10 | "babel-preset-stage-2": "^6.18.0", 11 | "babel-register": "^6.18.0", 12 | "chai": "^3.5.0", 13 | "mocha": "^3.1.2" 14 | }, 15 | "scripts": { 16 | "start": "babel-node index.js", 17 | "test": "mocha --watch" 18 | }, 19 | "author": "", 20 | "license": "ISC" 21 | } 22 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | fr.sample 8 | fp-sample 9 | 1.0-SNAPSHOT 10 | 11 | UTF-8 12 | 1.8 13 | 5.4.2 14 | 1.4.2 15 | 16 | 17 | 18 | 19 | maven-compiler-plugin 20 | 3.1 21 | 22 | ${java.version} 23 | ${java.version} 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | junit 32 | junit 33 | 4.12 34 | 35 | 36 | -------------------------------------------------------------------------------- /src/MyMovieCollectionSpecs.hs: -------------------------------------------------------------------------------- 1 | import Test.Hspec 2 | import Data.List 3 | 4 | data Movie = Movie { 5 | title :: String, 6 | year :: Int 7 | } deriving (Show, Eq) 8 | 9 | 10 | main = hspec $ do 11 | let movies = [ Movie { 12 | title= "The Matrix", 13 | year= 1999 14 | }, Movie { 15 | title= "A beautiful mind", 16 | year= 2001 17 | }, Movie { 18 | title= "Intouchable", 19 | year= 2011 20 | }, Movie { 21 | title= "Forest Gump", 22 | year= 1994 23 | }] 24 | 25 | describe "My movie collection search by name" $ do 26 | it "should return empty when none found" $ do 27 | findByTitle "Interstellar" movies `shouldBe` [] 28 | it "should return movie if title matches" $ do 29 | findByTitle "The Matrix" movies `shouldBe` [ Movie { 30 | title= "The Matrix", 31 | year= 1999 32 | }] 33 | it "should return all movies that matches" $ do 34 | findByTitle "o" movies `shouldBe` [ Movie { 35 | title= "Intouchable", 36 | year= 2011 37 | }, Movie { 38 | title= "Forest Gump", 39 | year= 1994 40 | }] 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /src/main/java/fp/Movie.java: -------------------------------------------------------------------------------- 1 | package fp; 2 | 3 | /** 4 | * Movie 5 | */ 6 | public class Movie { 7 | 8 | private String title; 9 | 10 | public String getTitle() { 11 | return title; 12 | } 13 | 14 | public void setTitle(String title) { 15 | this.title = title; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/fp/MovieFinder.java: -------------------------------------------------------------------------------- 1 | package fp; 2 | 3 | import java.util.List; 4 | import java.util.function.Function; 5 | import java.util.function.Predicate; 6 | import java.util.stream.Collectors; 7 | 8 | public class MovieFinder { 9 | 10 | /** 11 | * Return function that find movies by matching title inside a movie collection 12 | * @return 13 | */ 14 | public Function, List>> findByTitle() { 15 | 16 | Function, Function, List>> filter = moviePredicate -> movies -> movies.stream().filter(moviePredicate).collect(Collectors.toList()); 17 | 18 | Function> isInfixOf = title -> whole -> whole.contains(title); 19 | 20 | Function getTitle = movie -> movie.getTitle(); 21 | 22 | Function> matches = title -> movie -> isInfixOf.apply(title).compose(getTitle).apply(movie); 23 | 24 | return filter.compose(matches); 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/fp/MovieFinderTest.java: -------------------------------------------------------------------------------- 1 | package fp; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | public class MovieFinderTest { 11 | 12 | List movies; 13 | 14 | @Before 15 | public void setup() { 16 | movies = new ArrayList<>(); 17 | addMovie("The Matrix"); 18 | addMovie("A beautiful mind"); 19 | addMovie("Intouchable"); 20 | addMovie("Forest Gump"); 21 | } 22 | 23 | 24 | private void addMovie(String title) { 25 | Movie movie = new Movie(); 26 | movie.setTitle(title); 27 | movies.add(movie); 28 | } 29 | 30 | 31 | @Test 32 | public void findByTitle() { 33 | MovieFinder movieFinder = new MovieFinder(); 34 | 35 | List searchResult = movieFinder.findByTitle().apply("XXXXXXX").apply(movies); 36 | Assert.assertTrue("Should be empty", searchResult.isEmpty()); 37 | 38 | searchResult = movieFinder.findByTitle().apply("mind").apply(movies); 39 | Assert.assertEquals(1, searchResult.size()); 40 | Assert.assertEquals("A beautiful mind", searchResult.get(0).getTitle()); 41 | 42 | 43 | searchResult = movieFinder.findByTitle().apply("A beautiful mind").apply(movies); 44 | Assert.assertEquals(1, searchResult.size()); 45 | Assert.assertEquals("A beautiful mind", searchResult.get(0).getTitle()); 46 | 47 | 48 | searchResult = movieFinder.findByTitle().apply("a").apply(movies); 49 | Assert.assertEquals(3, searchResult.size()); 50 | Assert.assertEquals("The Matrix", searchResult.get(0).getTitle()); 51 | Assert.assertEquals("A beautiful mind", searchResult.get(1).getTitle()); 52 | Assert.assertEquals("Intouchable", searchResult.get(2).getTitle()); 53 | } 54 | 55 | 56 | } -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --compilers js:babel-register 2 | -------------------------------------------------------------------------------- /test/myMovieCollectionSpecs.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | import chai from "chai"; 3 | chai.should(); 4 | 5 | describe('My movie collection search by name', () => { 6 | it('should return empty when none found', () => { 7 | findByTitle("Interstellar", [ 8 | { 9 | title: "The Matrix", 10 | year: 1999 11 | }, 12 | { 13 | title: "A beautiful mind", 14 | year: 2001 15 | }, 16 | { 17 | title: "Intouchable", 18 | year: 2011 19 | }, 20 | { 21 | title: "Forest Gump", 22 | year: 1994 23 | } 24 | ]).should.be.empty; 25 | }); 26 | }); 27 | --------------------------------------------------------------------------------