시그마 삽질==six 시그마

일급객체(First-class citizen) 본문

프로그래밍/Programming stuff

일급객체(First-class citizen)

Ethan Matthew Hunt 2020. 3. 3. 21:58

일급객체(Wikipedia) 

 

In programming language design, a first-class citizen (also type, object, entity, or value) in a given programming language is an entity which supports all the operations generally available to other entities. These operations typically include being passed as an argument, returned from a function, modified, and assigned to a variable

 

일급함수(Wikipedia) 

 

In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures.[1] Some programming language theorists require support for anonymous functions (function literals) as well.

 

정의에 따르면

 

일급객체란

 

1. 인자값으로 객체를 전달 가능

2. 반환값으로 객체를 사용 가능

3. 변수나 데이터 구조안에 객체를 저장 가능

조건을 만족하는 객체

 

일급함수는 일급객체의 조건을 충족시키는 함수를 지칭함.

 

람다식은 일급객체의 특징을 가진 이름없는 함수임.

 

 

예시)

 

여러 언어로 구경해보자 :)

1) Swift

 

func foo(count:Int) -> String{
	return "정답은\(count+1)입니다"
}

let fn2=foo(base: 5) //"정답은 6입니다"

 

위는 단순히 함수의 결과값을 fn2 라는 상수에 할당하는 단순한 대입임

 

일급함수에서 말하는 변수에 대입한다는것은 함수의 결과값을 대입하는것이 아니라 함수 자체를 대입하는것

 

함수가 대입된 변수에 함수 호출 연산자 () 를 붙여서 함수를 호출가능

 

 

 

(1) arguments

func up(param : Int) ->Int{
	return param+1
}

func middle(base : Int, function fn : (Int) -> Int) -> Int {
	return fn(base)
}

middle(base:3, function:up) //4

 

(2) return value

func hello() -> String {
	return "This is hello()"
}

func toss() -> (Void) -> String{
	return hello
}

let t=toss()
t()  // "This is hello()

 

(3) variables 

func foo(count:Int) -> String{
	return "정답은\(count+1)입니다"
}

let fn2=foo

fn2(5) //"정답은 6입니다"

 

2) Kotlin

 

 

(1) arguments

fun main(){

    var result:Int
    result = pass({x,y->x+y}, 10,20)
    println(result)  //30
    
}

fun pass(sum: (Int, Int) -> Int, a: Int, B: Int): Int{
	return sum(a,b)
}

 

(2) return value

fun main(){
	 println(go())
}

val square: (Int)->Int = { x -> x * x }

fun go(): Int{
	return square(2) //4
}

 

 

(3) variables 

 

fun main(args: Array<String>){
    fun sum(){
    println("hello")
	}
	val result = ::sum
    
    result()  //hello
}

 

 

fun main(){
    var result:Int
    val multiple={x: Int, y: Int -> x*y}
    result = multiple(10,20)
    println(result)  //200
}

 

3) Javascript

 

 

(1) arguments

//함수 정의
function add(a,b,callback){   
   var result=a+b;
   callback(result);

}


//함수 호출

add(10,10,function(result){  
   console.log('파라미터로 전달된 콜백 함수 호출됨.');
   console.log('더하기(10,10)의 결과 :'+result);

});

 

$("#Btn").click(function(){
    alert("This is callback!");
})

 

 

(2) return value

var outerFunc = function(){
      console.log('outer!!!');
  return function(){
      console.log('return!!!!');
  };
};

var outter = outerFunc ();
outter ();    // outer!!! return!!!!

 

 

(3) variables 

function add(a,b){
	return a+b;
}

var add2=add;
console.log(add2(10,20));

 

'프로그래밍 > Programming stuff' 카테고리의 다른 글

애플리케이션 아키텍처와 객체지향  (0) 2020.04.12
우아한 객체지향  (0) 2020.04.11
모나드(Monad)  (0) 2020.03.04
클로저 Closure (computer programming)  (0) 2020.03.04
함수형 프로그래밍  (0) 2020.03.03
Comments