[Java] Java8, 람다 표현식(Lambda expressions)

많은 프로그래밍 언어에서 람다 표현식은 한번 사용하고 버릴 함수를 정의하기 위한 용도로 많이 사용됩니다. 물론 이 람다 표현식에 의해 만들어진 함수를 변수에 담아 두번 이상 사용할 수도 있지만… 어찌되었든, 람다는 빠르게 함수를 만들어 사용하고는, 버리는 용도로 사용합니다.

Java 8에서 람다 표현식을 지원하고 있는데요. 문자열 요소를 가지는 리스트를 정렬하는 예를 통해 람다 표현식을 살펴 보겠습니다. 정렬할 문자열 리스트로 사용할 names 객체 변수는 아래와 같습니다.

List names = 
    Arrays.asList("검소", "겸손", "명상", "욕심", "탐하는 마음", "경솔", "교만", "어질지못함");

람다 표현식이 아닌 우리가 이미 알고 있는 방식의 코드는 아래와 같습니다.

Collections.sort(names, new Comparator() {
    @Override
    public int compare(String a, String b) {
        return -b.compareTo(a);
    }
});

위의 Collections의 sort 함수의 두번째 인자에 1회용 목적으로 익명 객체를 생성해 던져주고 있습니다. 이 익명 객체를 생성하기 위한 인터페이스가 단 하나의 매서드에 대한 정의를 필요로 합니다. 이를 람다 표현식으로 바꾸면 아래와 같습니다.

Collections.sort(names, (String a, String b) -> {
    return -b.compareTo(a);
});

람다식을 컴파일로가 해석할 때 어떤 타입의 어떤 매서드를 정의하는지를 자동으로 추론해 줍니다. (염병할, 나보다 더 똑똑해!) 위의 람다식을 보면 함수의 코드가 한줄이므로 아래처럼 더 줄일 수 있습니다.

Collections.sort(names, (String a, String b) ->  -b.compareTo(a));

코드가 한줄이므로 {}을 제거할 수 있고, return 키워드를 제거해도 반환한다라는 개념을 이미 추론을 통해 파악하고 있습니다. (이제 니가 코딩해라, 난 뭐하냐?) 충분히 줄였다 싶을 람다 표현식을 더 줄일 수 있는데요. 어차피 어떤 함수를 람다식으로 정의하는지를 알고 있다면 해당 함수의 타입을 명시적으로 언급하지 않아도 추론이 가능할 것으므로 아래처럼 더 최적화할 수 있습니다.

Collections.sort(names, (a, b) -> -b.compareTo(a));

(이젠 나도 모르겠을 암호 코드.. ㅜ_ㅜ) 위의 내용은 람다식의 일부입니다. 람다에 좀더 깊이 들어가면.. 깜짝 놀라거임.. -O-;

CentOS 7에서, 인터넷이 되지 않는 환경에서 PostgreSQL, PostGIS 설치 절차

인터넷이 되지 않은 환경에서 PostgreSQL과 PostGIS를 설치하는 절차에 대해 정리한 글을 Keep해 둡니다. Windows에서 pgAdmin으로 접속하여 ‘CREATE EXTENSION postgis’ 명령 처리에 대한 내용까지 확인하였습니다.

먼저 PostgreSQL 설치 절차입니다.

# postgresq RPM Install
rpm -Uvh pgdg-centos94-9.4-2.noarch.rpm
rpm -ivh postgresql94-libs-9.4.9-1PGDG.rhel7.x86_64
rpm -ivh postgresql94-9.4.9-1PGDG.rhel7.x86_64.rpm
rpm -ivh postgresql94-server-9.4.9-1PGDG.rhel7.x86_64.rpm
rpm -ivh postgresql94-devel-9.4.9-1PGDG.rhel7.x86_64.rpm
rpm -ivh libxslt-1.1.28-5.el7.x86_64.rpm
rpm -ivh postgresql94-contrib-9.4.9-1PGDG.rhel7.x86_64.rpm

# init DB
./postgresql194-setup initdb

# OS start, start PostgreSQL Server
systemctl enable postgresql-9.4

# Start Postgresql service
systemctl enable postgresql-9.4.service
systemctl start postgresql-9.4.service

# Firewall allow port 5432
firewall-cmd --zone=public --add-port=5432/tcp

# allow connection for all client IP
vi postgresql.conf
listen_addresses = "*"

# Configuring PostgreSQL Authentication
su - postgres
psql
\password postgres
\q

# edit pg_hba.conf
local all all peer
-> Do change
local all all md5

host all all 127.0.0.1/32 ident
-> Do change
host all all 0.0.0.0/0 md5

host all all ::1/128 ident
-> Do change
host all all ::1/128 md5

# restart Postgresql service
systemctl stop postgresql-9.4.service
systemctl start postgresql-9.4.service

다음은 PostGIS 설치 절차입니다.

# proj 설치
tar zxf proj-4.8.0.tar.gz
cd proj-4.8.0
./configure
make && make install

# geos 컴파일
tar xvf geos-3.4.2.tar.bz2
cd geos-3.4.2
./configure
make && make install

# libxml 설치 (rpm에서 package 총돌시 --replacefiles 옵션을 사용)
rpm -ivh ./libxml2-2.9.1-5.el7_1.2.x86_64.rpm
rpm -ivh ./xz-libs-5.1.2-12alpha.el7.x86_64.rpm
rpm -ivh ./xz-devel-5.1.2-12alpha.el7.x86_64.rpm
rpm -ivh ./zlib-libs-1.2.7-15.el7.x86_64.rpm
rpm -ivh ./zlib-devel-1.2.7-15.el7.x86_64.rpm
rpm -ivh ./libxml2-devel-2.9.1-5.el7_1.2.x86_64.rpm

# json 설치
rpm -ivh ./json-c-0.11-4.el7_0.x86_64.rpm

# gdal 컴파일
tar zxf gdal-1.9.2.tar.gz
cd gdal-1.9.2.tar.gz
./configure
make && make install

# postgis 컴파일
tar zxf postgis-2.0.3SVN.tar.gz
cd postgis-2.1.8
./configure --with-pgconfig=/usr/pgsql-9.4/bin/pg_config --with-raster
make && make install

# 모듈 파일(.so) 업데이트
echo /usr/local/lib >> /etc/ld.so.conf
ldconfig

# postgis 배포
cd extensions/postgis
make clean
make && make install
cd ..
cd postgis_topology
make clean
make && make install

# restart Postgresql service
systemctl restart postgresql-9.4.service

인터넷이 되지 않는 환경에서 PostgreSQL과 PostGIS 설치를 하는 분들에게 도움이 되시기 바랍니다.