Sinks and Sources (sink와 source)
원본: Computation Layer 문서(Sinks and Sources)
CL might look different from other programming languages you're used to. Instead of operating as a list of commands that run one after another, CL allows you to wire different pieces together to keep their values consistent. For example, a = b means that a is always the value of b. The value of a will change whenever the value of b does. This is similar to how spreadsheets work. It's also how the Desmos calculator works.
CL은 익숙한 다른 프로그래밍 언어와 달라 보일 수 있다. CL은 명령을 하나씩 차례로 실행하는 목록이 아니라, 여러 부분을 서로 연결해 값이 늘 일치하도록 만든다. 예를 들어 a = b는 a가 언제나 b의 값이라는 뜻이다. b의 값이 바뀌면 a의 값도 함께 바뀐다. 스프레드시트가 작동하는 방식과 비슷하고, Desmos 계산기도 이렇게 작동한다.
"Sinks" and "Sources" are the way to get values into and out of components. For example, the statement number("m"): exp1.numericValue means that the value of m inside of this Graph component equals the computed value of the Expression Input component named "exp1." As a student types into that Expression Input, the value of m in the graph will update immediately.
sink와 source는 구성요소(component)에 값을 넣고 구성요소에서 값을 꺼내는 방법이다. 예를 들어 number("m"): exp1.numericValue라는 문장은 이 그래프 구성요소 안의 m 값이 "exp1"이라는 수식 답변 구성요소의 계산값과 같다는 뜻이다. 학생이 그 수식 답변에 입력하는 동안 그래프의 m 값이 곧바로 바뀐다.
Sinks
sink
Use sinks to inject values into components. Sinks are colored teal and are always followed by a colon. For example, you could make a graph that plots y=mx+b and then use the graph's "number" sink to set values for the variables m and b.
sink는 구성요소에 값을 넣을 때 쓴다. sink는 청록색으로 표시되고 뒤에 항상 콜론(:)이 붙는다. 예를 들어 y=mx+b를 그리는 그래프를 만든 뒤 그래프의 "number" sink로 변수 m과 b의 값을 정할 수 있다.
# Set the values of m & b in the graph number("m"): 1.5 number("b"): 2
Variables
변수
You can define variables within a script. This makes your code more reusable and helps you avoid repeating the same code. Note that variables look very similar to sinks but behave very differently.
스크립트 안에서 변수를 정의할 수 있다. 변수를 쓰면 코드를 여러 곳에서 다시 쓸 수 있고 같은 코드를 되풀이하지 않아도 된다. 변수는 sink와 모양이 아주 비슷하지만 작동은 전혀 다르다는 점에 주의한다.
Suppose you wanted the submitLabel and initialText on an input to both say, "Hello world!"
입력 구성요소의 제출 버튼 라벨(submitLabel)과 초기에 입력된 내용(initialText)이 둘 다 "Hello world!"(안녕, 세상!)가 되게 하고 싶다고 하자.
#if you change this line, both submitLabel and initialText will change. customString = "Hello world!" submitLabel: customString initialText: customString
Pro tip: You can reference variables defined in other components by using the "script" keyword. For example:
팁: "script" 키워드를 쓰면 다른 구성요소에서 정의한 변수를 가져올 수 있다. 예를 들면 다음과 같다.
choiceContent(1): input1.script.customString
Sources
source
A source is a value that you can pull out of a component and is typically based on work a student has done. To use a component's source, name the component and the use that name in your script.
source는 구성요소에서 꺼내 쓸 수 있는 값이고, 대개 학생이 한 작업을 바탕으로 한다. 구성요소의 source를 쓰려면 구성요소에 이름을 붙이고 스크립트에서 그 이름을 쓴다.
For example, let's say that you have a button that you've named btn1. You can use its sources in any script anywhere in the activity, like this:
예를 들어 btn1이라는 이름을 붙인 버튼이 있다고 하자. 이 버튼의 source는 활동 안 어느 스크립트에서든 다음과 같이 쓸 수 있다.
#pressCount is a simple number number("b"): btn1.pressCount #the "timeSincePress" source asks for a duration. #this will stop reporting after 10 seconds. number("a"): btn1.timeSincePress(10)
Pro tip: If you want to access a source from a component you're in, you don't need to name the component. In a button Component's script, for example, just write label: this.pressCount.
팁: 스크립트를 쓰고 있는 그 구성요소의 source를 쓸 때는 구성요소 이름을 적지 않아도 된다. 예를 들어 버튼 구성요소의 스크립트에서는 label: this.pressCount라고만 쓰면 된다.