Skip to content

Commit

Permalink
#15 - корректировка примеров
Browse files Browse the repository at this point in the history
  • Loading branch information
Stepami committed Jul 20, 2024
1 parent 5ff32a5 commit c621031
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 47 deletions.
15 changes: 6 additions & 9 deletions samples/equals.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
type withEquals = {
prop: number;
equals: (withEquals) => boolean;
}

let obj1 = {
function equals(obj: withEquals, that: withEquals) {
return obj.prop == that.prop
}

let obj1: withEquals = {
prop: 1;
equals => (that: withEquals): boolean {
return prop == (that.prop)
};
}

let obj2 = {
let obj2: withEquals = {
prop: 2;
equals => (that: withEquals): boolean {
return prop == (that.prop)
};
}

let res = obj1.equals(obj2)
Expand Down
42 changes: 21 additions & 21 deletions samples/linkedlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,27 @@ type node = {
}
type list = {
head: node;
append: (number,) => void;
getOdd: () => number[];
}

function append(lst: list, item: number) {
let tail: node = lst.head
while (tail.next != null) {
tail = tail.next
}
tail.next = makeNode(item)
}

function getOdd(lst: list): number[] {
let result: number[]
let n = lst.head
while (n != null) {
if (n.data % 2 != 0) {
let i = n.data
result = result ++ [i]
}
n = n.next
}
return result
}

function makeNode(item: number): node {
Expand All @@ -17,25 +36,6 @@ function makeNode(item: number): node {

let linkedList: list = {
head: makeNode(1);
append => (item: number) {
let tail: node = head
while ((tail.next) != null) {
tail = tail.next
}
tail.next = makeNode(item)
};
getOdd => (): number[] {
let result: number[]
let n = head
while (n != null) {
if ((n.data) % 2 != 0) {
let i = n.data
result = result ++ [i,]
}
n = n.next
}
return result
};
}

linkedList.append(3)
Expand Down
14 changes: 7 additions & 7 deletions samples/settable.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
type settable = {
prop: string;
setprop: (string) => void;
}

function setprop(obj: settable, str: string) {
obj.prop = str
if (obj.prop == "1") {
print("prop is one")
}
}

let obj: settable = {
prop: "prop";
setprop => (str: string) {
prop = str
if (prop == "1") {
print("prop is one")
}
};
}

obj.setprop("1")
Expand Down
14 changes: 10 additions & 4 deletions samples/summator.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
let summator = {
type summable{
x: number;
y: number;
}

function sum(obj: summable): number {
return obj.x + obj.y
}

let summator: summable = {
x: 1;
y: 2;
sum => (): number {
return x + y
};
}

let s = summator.sum()
Expand Down
17 changes: 12 additions & 5 deletions samples/this.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
let obj = {
type ObjType = {
num: number;
flag: boolean;
str: string;
}

function toString(obj: ObjType): string {
let s = "object obj:\n"
return s + (obj as string)
}

let obj: ObjType = {
num: 1;
flag: true;
str: "field";
toString => (): string {
let s = "object obj:\n"
return s + (this as string)
};
}

print(obj.toString())
3 changes: 2 additions & 1 deletion samples/vec2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ function vlensquared(v: vec2d): number {

let v = makeVec(3, 4)
let l = vlensquared(v)

print(l as string)
l = v.vlensquared()
print(l as string)

0 comments on commit c621031

Please sign in to comment.