This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding:utf-8 -*- | |
| class Parent(object): | |
| def __init__(self, name): | |
| self.name = name | |
| print('My name is {0}'.format(name)) | |
| def show(self): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object FuncThinking1 extends App { | |
| def firstIndexOfAny(input: String, searchChars: Seq[Char]): Option[Int] = { | |
| def indexedInput = (0 until input.length).zip(input) | |
| val result = for (pair <- indexedInput; char <- searchChars; if (char == pair._2)) | |
| yield (pair._1) | |
| if (result.isEmpty) | |
| None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class PersonList { | |
| public String cleanNames(List<String> listOfNames) { | |
| StringBuilder builder = new StringBuilder(); | |
| for(int i = 0; i < listOfNames.size(); i++) { | |
| if(listOfNames.get(i).length() > 1) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object FuncThinking2 extends App { | |
| val employees = List("james", "s", "kevin", "paul") | |
| val result1 = employees.filter(_.length > 1).map(_.capitalize).reduce(_ + "," + _) | |
| println(result1) | |
| // 동일한 결과, 최종 문자열 조인의 차이 | |
| val result2 = employees.filter(_.length > 1).map(_.capitalize).mkString(",") | |
| println(result2) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.concurrent.TimeUnit.NANOSECONDS | |
| def time[T](f: => T): T = { | |
| val start = System.nanoTime() | |
| val ret = f | |
| val end = System.nanoTime() | |
| // scalastyle:off println | |
| println(s"Time taken: ${NANOSECONDS.toMillis(end - start)} ms") | |
| // scalastyle:on println | |
| ret | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val employees = List("james", "s", "kevin", "paul") | |
| // 서술형 프로그래밍 | |
| val result1 = employees.filter(_.length > 1).map(_.capitalize).reduce(_ + "," + _) | |
| // par을 추가하여 병령처리 | |
| val result2 = employees.par.filter(_.length > 1).map(_.capitalize).reduce(_ + "," + _) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // a 와 b를 더하는 함수를 반환 | |
| def adder(a: Int) = (b: Int) => { | |
| a + b | |
| } | |
| // a의 값에 각각 5와 7을 바인딩, 함수가 해제하기 전까지 데이터를 저장 | |
| def addFive = adder(5) | |
| def addSeven = adder(7) | |
| println(addFive(2)) // 7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def modN(n: Int)(x: Int) = ((x % n) == 0) | |
| // modN함수를 커링을 이용하여 n 값이 정해진 변수로 호출 | |
| def modOne:Int => Boolean = modN(1) | |
| def modTwo = modN(2) _ | |
| println(modOne(4)) // true | |
| println(modTwo(4)) // true | |
| println(modTwo(5)) // false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object memoizeSample extends App { | |
| // 처리 시간 출력 | |
| import java.util.concurrent.TimeUnit.NANOSECONDS | |
| def time[T](f: => T): T = { | |
| val start = System.nanoTime() | |
| val ret = f | |
| val end = System.nanoTime() | |
| println(s"Time taken: ${NANOSECONDS.toNanos(end - start)} nonos") | |
| ret | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package functional.stage1; | |
| import java.util.HashSet; | |
| import java.util.Iterator; | |
| import java.util.Set; | |
| /** | |
| * 명령형 자연수 분류기 | |
| * | |
| * @author whitebeard-k |