Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0ecb7c3

Browse files
committed
Add _clone (shallow copy) and update _set to use shallow copy and
recursively modify nodes (seehttps://blog.klipse.tech/javascript/2021/02/26/structural-sharing-in-javascript.html)
1 parent126d9d2 commit0ecb7c3

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

‎challenges/golang/godash.go‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,20 @@ func _set(data, sel, v any) any {
5656
returnret
5757
}
5858

59-
mdata:=_cloneDeep(data).(d)
60-
6159
ifk,ok:=sel.(string);ok {
60+
mdata:=_clone(data).(d)
6261
mdata[k]=v
6362
returnmdata
6463
}
6564

6665
path:=sel.(p)
67-
lpath:=len(path)
68-
cur:=mdata
6966

70-
fori,k:=rangepath {
71-
ifdd,ok:=cur[k].(d);ok {
72-
cur=dd
73-
}elseifi==lpath-1 {
74-
cur[k]=v
75-
}else {
76-
panic("invalid value for "+k)
77-
}
67+
k:=path[0]
68+
iflen(path)>1 {
69+
v=_set(data.(d)[k],path[1:],v)
7870
}
7971

80-
returnmdata
72+
return_set(data,k,v)
8173
}
8274

8375
func_omit(data,selany)any {
@@ -215,6 +207,18 @@ func _reduce(in any, f reducer, acc any) any {
215207
returnacc
216208
}
217209

210+
func_clone(vany)any {
211+
ifd,ok:=v.(d);ok {
212+
returnclonemap(d,false)
213+
}
214+
215+
ifl,ok:=v.(l);ok {
216+
returncloneslice(l,false)
217+
}
218+
219+
returnv
220+
}
221+
218222
func_cloneDeep(vany)any {
219223
ifd,ok:=v.(d);ok {
220224
returnclonemap(d,true)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp