// 2015-02-13 13:40:47 (금요일)
locationData = "Liverpool, England: 53d 25m 0s N 3d 0m 0s"
myRegularExpression = /([a-zA-Z]+), ([a-zA-Z]+): ([0-9]+). \
([0-9]+). ([0-9]+). ([A-Z]) ([0-9]+). ([0-9]+). ([0-9]+)./
matcher = ( locationData =~ myRegularExpression )
println locationData
println locationData.getClass()
println matcher.matches()
println matcher.getCount()
println matcher[0]
println matcher[0][0]
println matcher[0][1]
println matcher[0][6]
// 결과가 이차원배열에 들어간다는 사실.
// Non-matching group
names = [
"Graham James Edward Miller",
"Andrew Gregory Macintyre",
"No MiddleName"
]
printClosure = {
matcher = (it =~ /(.*?)(?: .*)* (.*)/)
if ( matcher.matches())
println( matcher[0][2]+", " + matcher[0][1])
}
names.each(printClosure)
// 문자열 바꾸기
excerpt = "At school, Harry had no one. Everybody knew that \
Dudley's gang hated that odd Harry Potter "+
"in his baggy old clothes and broken glasses, \
and nobody liked to disagree with Dudley's gang.";
println excerpt
matcher = (excerpt =~ /Harry Potter/)
excerpt = matcher.replaceAll("Tanya Grotter")
matcher = (excerpt =~ /Harry/)
excerpt = matcher.replaceAll("Tanya")
println("Publish it! " + excerpt)
//
// Greedy operator
// Reluctant operator
//
|
|---|
2015년 2월 12일 목요일
groovy 연습 4
groovy 연습 3
// 2015-02-13 11:25:27 (금요일)
// 그루비의 튜토리얼 문서가 부실하다는 느낌을 지울수없다.
"potatoe" ==~ /potatoe/
println "potatoe" ==~ /potatoe/
"potatoe" ==~ /potatoe?/
println "potatoe" ==~ /potatoe?/
"potato" ==~ /potatoe?/
println "potato" ==~ /potatoe?/
"motato" ==~ /potatoe?/
println "motato" ==~ /potatoe?/
// 함수를 정의할때 def 를 쓴다.
def checkSpelling( spellingAttempt, spellingRegularExpression)
{
if (spellingAttempt ==~ spellingRegularExpression)
{
println("철자를 맞게 썼음.")
} else {
println("철자가 틀림. 다시 해보기 바람.")
}
}
theRegularExpression = /Wisniewski/
checkSpelling("Wisniewski", theRegularExpression)
checkSpelling("Wisnewski", theRegularExpression)
def 철자검사( 철자, 정규식)
{
if (철자 ==~ 정규식)
{
println("철자를 맞게 썼음.")
}
else
{
println("철자가 틀림. 다시 해보기 바람.")
}
}
theRegularExpression = /Wisniewski/
철자검사("Wisniewski", theRegularExpression)
철자검사("Wisnewski", theRegularExpression)
// 함수를 정의할때 def 를 쓴다.
// 일반함수 정의와 클로져함수 정의의 차이점을 잘봐두자.
def fac1(n) { n == 0 ? 1 : n * fac(n-1) }
def fac2 = { n -> n == 0 ? 1 : n * call(n-1) }
println fac2(4)
def message( contents) {
println contents
}
message( "연락바람.")
//
// 구루비 스크립트 파일명을 정할때, 숫자로 시작하는 파일명을하면
// 함수나 클래스에서 에러가 난다.
// 또한 파일명과 같은 클래스이름을 만들면 에러가 난다.
// 파일명에 주의할것.
//
/*
Regular Expression
a? a 가 0 또는 1 개 있음. 'a' 또는 빈문자열
a* a 가 0 또는 여러개 있음. 빈문자열 또는 'a', 'aa', 'aaa', ...
a+ a 가 1 또는 여러개 있음. 'a', 'aa', 'aaa', ...
a|b a 또는 b 'a' 또는 'b'
. 어떤 문자가 하나 있음. 'a', 'q', 'l', '_', '+', ...
[woeirjsd] 나열된문자중 하나가 있음. 'w','o','e','i','r','j','s'
[1-9] 주어진 범위의 문자중 하나가 있음. '1','2','3','4','5','6','7','8','9'
[^13579] 나열된문자가 아닌것. 짝수, 또는 다른 문자.
(ie) 식의 그룹 'ie'
^a a 가 줄의 맨처음에 있음. 'a'
a$ a 가 줄의 맨끝에 있음. 'a'
*/
|
|---|
groovy 연습 2
|
|---|
2015년 2월 11일 수요일
groovy 연습 1
// 그루비를 배워보자.
/* 주석이 되나. */
// 그래. 이것이 그루비 코드다.
// 2015-02-12 11:34:45 (목요일)
println("Hello world")
println("안녕, 세상아")
// 한글로, 이런 인사는 어색하다. 낯설다. 이런말은 아예 쓰질 않는다.
// "안녕, 세상아"라니...
// 차라리, 이런 표현이 더 자연스럽지 않을까.
println("세계인들이여 안녕.")
println("지구여 안녕.")
// 역시나, 잘 쓰지 않는 문장이다.--;
for ( a in this.args) {
println("인자: " + a)
}
//
// 나 자신이 되어라.
// 정해진 길은 없다.
// groovy 를 학습하는데, 이미검증된 단하나의 최고의 방법은
// 존재하지 않는다.
// 내가 가는 길이, 내게는 최선의 길이다.
//
// 음..
// 타입을 쓰지않는다.
// C++ 언어에 찌들어있는 내게,
// 이 언어는 마치 파이썬처럼 보인다.
x = 1
println x
// 음..
// 한글변수는?
한글변수 = 2
print "한글변수 : "
println 한글변수
// 음..
// java 에서 한글변수가 되는것처럼.
// groovy 에서도 한글변수가 되는것을 확인한다.
// java 언어의 모든 라이브러리를 쓸수있다는 점.
// 그루비 언어가 자랑질을 하고있다.
x = new java.util.Date()
println x
x = -3.141592
println x
x = false
println x
x = "안녕하신가."
println x
//
// 순간순간. 모든순간에.
// 나의 창의와 독창성이 발현되지않으면 안된다.
//
// list 라는 것.
// 순서가 정해진 배열.
myList = [1776, -1, 33, 99, 0, 234752394857]
println myList
// C++ 언어의 배열처럼, 인덱스는 0 부터 시작한다.
println myList[0]
// C++ 언어의 배열과는 달리, 그루비 리스트는 메서드를 가지고있다.
// 더 우월하다고 평가할수있다.
println myList.size()
// map 이라는것.
// 순서가 없는, 불균질, 이름있는 데이터.
// 파이선의 딕셔너리.
// C++ 에서는 불균질(heterogeneous) 데이터는 표현할수 없다.
// C++ 에서는 모든 배열의 element 는 같은 타입이어야한다.
// C++ 에서는 모든 리스트의 element 는 같은 타입이어야한다.
// 그러나, 현대언어에서는 불균질 타입이 가능하다.
scores = [ "Brett":100
, "Pete":"Did not finish"
, "Andrew":86.87934]
println scores
// 나는 왜 위와 같은 코드읽기를 힘들어하는가.
// 한글이 아니기 때문이다.
// 나의 언어능력은 점점더 떨어지고 있다.
// 나는 더 읽기 쉬운 코드가 필요하다.
// 나는 더 눈에 잘 들어오는 코드가 필요하다.
점수 = [ "김민기":100, "김대익":"아직 안했음.", "김동현":86.87934]
println 점수
// 이제야, 이 코드가 눈에 들어온다.
// 이제야, 이 코드가 만만해 보인다. "별거 없네!"라는 자신감이 생긴다.
// 한글이 아니고서는, 코드를 읽을 수 없다. 난독증이다.
// map 의 요소 하나만 가져와 본다.
println scores["Pete"]
println scores.Pete
// 이 코드를 여전히 읽을수없다.
// 영어 울렁증이다.
// 음..
println 점수["김대익"]
println 점수.김대익
// 이 코드는 읽을 수있다.
// 나는 한글이 아니면, 코드를 읽을수없다.
scores["Pete"] = 3
println scores["Pete"]
println scores
점수["김대익"] = 3
println 점수["김대익"]
println 점수
// 비어있는 리스트, 비어있는 맵.
emptyList = []
emptyMap = [:]
println emptyList
println emptyMap
println emptyList.size()
println emptyMap.size()
// 역시나 난독이다. 한눈에 들어오지 않는다.
// 다시 한글로.
공갈리스트 = []
뻥맵 = [:]
println 공갈리스트
println 뻥맵
println 공갈리스트.size()
println 뻥맵.size()
// if 문.
// 조건에 따라, 다른 코드를 실행.
amPM = Calendar.getInstance().get(Calendar.AM_PM)
if( amPM == Calendar.AM)
{
println("Good morning")
} else {
println("Good evening")
}
// 용어: "else" block, "then" block
amPM = Calendar.getInstance().get(Calendar.AM_PM)
if( amPM == Calendar.AM)
{
println("Have another cup of coffee.")
}
// 한글
때 = Calendar.getInstance().get(Calendar.AM_PM)
if( 때 == Calendar.AM)
{
println("좋은 아침입니다.")
} else {
println("안녕히 주무세요.")
}
시간 = Calendar.getInstance().get(Calendar.AM_PM)
if( 시간 == Calendar.AM)
{
println("커피 한잔 할까.")
}
// 역시나, 한글화하는데는 한계가 있다.
// 영어를 읽어야 한다.
// boolean 식.
// 진리식.
myBooleanVariable = true
println myBooleanVariable
// null 이라는 값을 쓸수있다.
suvMap = ["Acura MDX":"\$36,700"
, "Ford Explorer":"\$26,845"
, "Hummer H3":"\$33.890"]
if( suvMap["Hummer H3"] != null)
{
println("A Hummer H3 will set you back "
+ suvMap["Hummer H3"])
}
// class 이름을 출력해볼수있다.
myBooleanVar = true
println myBooleanVar.getClass()
myStrVar = "글자"
println myStrVar.getClass()
myNumVar = 33
println myNumVar.getClass()
myBigNum = 3.14
println myBigNum.getClass()
/*
class java.lang.Boolean
class java.lang.String
class java.lang.Integer
class java.math.BigDecimal
// java 의 인프라를 그대로 가져와 사용하는 모습이다.
// groovy 언어의 안정성을 jvm 을 통해 보장받을수있다.
// java 에 익숙함. 친숙함을 그대로 이용할수있다.
*/
|
|---|
피드 구독하기:
글 (Atom)
Firefly Algorithms
firefly algorithm 001 Firefly Algorithms ¶ 반딧불 알고리즘 번역 요약 ¶ References [1] X. S. Y...
-
// 그루비를 배워보자. /* 주석이 되나. */ // 그래. 이것이 그루비 코드다. // 2015-02-12 11:34:45 (목요일) println( "Hello world" ) printl...
-
firefly algorithm 001 Firefly Algorithms ¶ 반딧불 알고리즘 번역 요약 ¶ References [1] X. S. Y...
-
In [6]: # # 나는 julia 를 사랑하는가. # In [ ]: # Variable # 변수를 알아...