Lambda 함수 사용 예제

|

Lambda 함수 사용 예제

함수 파라메터로 함수 사용하는 방법

아래 예시 코드는 add_with_lambda라는 함수의 파라메터로 함수를 전달하는 예제 코드입니다. add_one, add_two, add_three의 함수를 전달하고 있습니다.

def add_with_lambda(a, b, lambda_func):
  sum = lambda_func(a) + lambda_func(b)
  return lambda_func(sum)


def add_one(a):
  return a + 1


def add_two(a):
  return a + 2


def add_three(a):
  return a + 3


if __name__ == "__main__":
  print("Lambda Example")

  print(add_with_lambda(3, 5, add_one))
  print(add_with_lambda(3, 5, add_two))
  print(add_with_lambda(3, 5, add_three))

함수를 파라메터로 전달하는 기능은 상당히 유용하며, 함수의 재활용성을 높이고 가독성을 높이는데 도움을 줍니다. 하지만, 인자로 사용할 함수를 매번 다른 이름으로 하나씩 만드는 것은 번거로운 일입니다. 이런 경우 람다 함수를 사용하면 코드를 훨씬 더 깔끔하게 만들 수 있습니다.

참고로 람다 함수(Lambda Function)는 ‘익명 함수’라는 의미입니다. 별도로 이름이 없더라도 함수를 사용할 수 있다는 의미입니다.


람다 함수 사용 예제

위의 예제 코드를 람다 함수를 사용해서 변환한 모습입니다. 코드가 훨씬 더 깔끔해졌습니다.

def add_with_lambda(a, b, lambda_func):
  sum = lambda_func(a) + lambda_func(b)
  return lambda_func(sum)


if __name__ == "__main__":
  print("Lambda Example")

  print(add_with_lambda(3, 5, lambda a: a + 1))
  print(add_with_lambda(3, 5, lambda a: a + 2))
  print(add_with_lambda(3, 5, lambda a: a + 3))

Web SSH Server 설치 방법

|

Web 브라우저에서 접속할 수 있는 SSH 서버 설치

shellinabox라는 프로그램을 통해서 웹 브라우저에서 접속할 수 있는 SSH 서버를 설치할 수 있습니다.


shellinabox 설치

sudo apt-get install shellinabox


포트 변경 등

sudo nano /etc/default/shellinabox


서비스 실행/중지

sudo service shellinabox stop


상태 조회

sudo netstat -nap | grep shellinabox


css 적용

만약 css를 적용하고 싶으면 먼저 서비스를 중지시킨다음 터미널에서 다음과 같이 입력하면 됩니다.

shellinaboxd --css "/home/snowdeer/white-on-black.css"

또는 아예 config 파일에 설정하면 컴퓨터를 재실행해도 항상 적용이 되기 때문에 더 편리합니다.

sudo nano /etc/default/shellinabox

...

SHELLINABOX_ARGS="--no-beep --css /home/snowdeer/white-on-black.css"


white-on-black.css 파일

shellinabox 기본값은 하얀 바탕에 검은 글씨인데, 만약 반대 색상을 사용하고 싶으면 아래 css 파일을 적용해서 테마를 변경할 수 있습니다.

#vt100 #cursor.bright {
  background-color: white;
  color:            black;
}

#vt100 #cursor.dim {
  background-color: black;
  opacity:          0.2;
  -moz-opacity:     0.2;
  filter:           alpha(opacity=20);
}

#vt100 #scrollable {
  color:            #ffffff;
  background-color: #000000;
}

#vt100 #scrollable.inverted {
  color:            #000000;
  background-color: #ffffff;
}

#vt100 .ansiDef {
  color:            #ffffff;
}

#vt100 .ansiDefR {
  color:            #000000;
}

#vt100 .bgAnsiDef {
  background-color: #000000;
}

#vt100 .bgAnsiDefR {
  background-color: #ffffff;
}

#vt100 #scrollable.inverted .ansiDef {
  color:            #000000;
}

#vt100 #scrollable.inverted .ansiDefR {
  color:            #ffffff;
}

#vt100 #scrollable.inverted .bgAnsiDef {
  background-color: #ffffff;
}

#vt100 #scrollable.inverted .bgAnsiDefR {
  background-color: #000000;
}

SSH 설치 방법

|

SSH Server 설치 방법


설치

다음 명령어를 이용해서 설치할 수 있습니다.

sudo apt-get install openssh-server


재실행

보통 설치와 함께 서비스가 같이 실행되기 때문에 다른 설정은 필요없습니다. 하지만, 만약 ssh 서버를 재시작하고 싶으면 다음 명령어를 사용하면 됩니다.

sudo /etc/init.d/ssh restart


포트 변경

sudo nano /etc/ssh/sshd_config 

CMake 업그레이드 방법

|

CMake 업그레이드 방법

다음 명령어를 이용해서 cmake를 업그레이드 할 수 있습니다.

sudo -E add-apt-repository -y ppa:george-edison55/cmake-3.x
sudo -E apt-get update
sudo apt-get install cmake

다만, 위 방법으로는 3.5만 받아집니다. 2018년 4월 10일 기준으로 cmake 최신 버전은 3.11 입니다. 최신 버전을 받기 위해서는 다음 명령어를 이용합니다.

sudo apt remove cmake

https://cmake.org/files/v3.11/cmake-3.11.0.tar.gz
tar -zxvf cmake-3.11.0.tar.gz

cd cmake-3.11.0
./bootstrap
make
sudo make install

cmake --version

Constraint Layout를 활용한 Circle Layout

|

Constraint Layout를 활용한 Circle Layout

Constraint Layout beta 3 부터 원형 레이아웃을 지원하기 시작했습니다. gradle에서 다음 종속성을 추가해주면 beta 3를 사용할 수 있습니다.


gradle 설정

dependencies {
  implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta3'
}


사용 방법

image

위 그림을 참고해서 다음과 같은 레이아웃을 작성하면 원형 레이아웃을 만들 수 있습니다.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">

  <ImageView
    android:id="@+id/center_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher_round"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

  <ImageView
    android:id="@+id/image_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher_round"
    app:layout_constraintCircle="@+id/center_image"
    app:layout_constraintCircleAngle="0"
    app:layout_constraintCircleRadius="140dp"/>

  <ImageView
    android:id="@+id/image_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher_round"
    app:layout_constraintCircle="@+id/center_image"
    app:layout_constraintCircleAngle="120"
    app:layout_constraintCircleRadius="140dp"/>

  <ImageView
    android:id="@+id/image_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher_round"
    app:layout_constraintCircle="@+id/center_image"
    app:layout_constraintCircleAngle="240"
    app:layout_constraintCircleRadius="140dp"/>
</android.support.constraint.ConstraintLayout>