assembly.jc 2019-5-30 13:37
一個簡單的例子:
[quote]
private static Pair< Perceptron, Sample> train(
double[] w, double b, Vector[] postData, Vector[] zeroData
) {
Perceptron trainer = Perceptron.of(w, b);
Sample trainData = new Sample(postData, zeroData);
trainer.setDebug();
train(trainer, trainData);
return(new Pair(trainer, trainData));
}
[/quote]
In Java, 要回傳 2 個唔同型態的 Objects,可以用 Array,如: return(new Object[] {trainer, trainData}),但咁做,compile time check 唔到型態,而且要用個陣要 casting。好 d 嘅方法,好似以上個例子咁用 Generic Type。但如果回傳 3, 4, 5 ...個唔唔同型態的 Objects,可能就要定義
[quote]
public class<T1, T2, T3> Tuple3 {...}
public class<T1, T2, T3, T4> Tuple4 {...}
public class<T1, T2, T3, T4, T5> Tuple5 {...}
...
[/quote]
咁做好麻煩,你地用開嘅 OO Language,有乜好嘅解決方法?
form5 2019-5-31 23:18
冇乜好辦法,多過三個好樣衰
assembly.jc 2019-5-31 23:52
[quote]原帖由 [i]form5[/i] 於 2019-5-31 11:18 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500254439&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
冇乜好辦法,多過三個好樣衰 [/quote]
見到 5 兄,諗起 C#,google 了一下,原來有 Tuple 簡化版,都做得幾好喎:
[url=https://www.tutorialsteacher.com/csharp/valuetuple]https://www.tutorialsteacher.com/csharp/valuetuple[/url]
form5 2019-6-1 00:25
[quote]原帖由 [i]assembly.jc[/i] 於 2019-5-31 11:52 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500256032&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
見到 5 兄,諗起 C#,google 了一下,原來有 Tuple 簡化版,都做得幾好喎:
[url=https://www.tutorialsteacher.com/csharp/valuetuple]https://www.tutorialsteacher.com/csharp/valuetuple[/url] [/quote]
you can if you want that in C#
in dotnet core you don't have to add ValueTuple reference explicitly.
I prefer to return dummy class wrapper .[code] class Program
{
static void Main(string[] args)
{
var vm = FormViewModel.Create();
var vm2 = FormViewModel.CreateTuple();
// vm2.txtBoxVm ....
}
}
public class FormViewModel
{
public OptionViewModel OptionViewModel { get; set; }
public TextBoxViewModel TextBoxViewModel { get; set; }
public static Func < FormViewModel > Create = ( )
=> new FormViewModel()
{
OptionViewModel = new OptionViewModel(),
TextBoxViewModel = new TextBoxViewModel()
};
public static (OptionViewModel optVm, TextBoxViewModel txtBoxVm) CreateTuple ()
{
return (new OptionViewModel(), new TextBoxViewModel());
}
}
public class OptionViewModel { }
public class TextBoxViewModel { }[/code]
form5 2019-6-1 00:26
class Program
{
static void Main(string[] args)
{
var vm = FormViewModel.Create();
var vm2 = FormViewModel.CreateTuple();
// vm2.txtBoxVm ....
}
}
public class FormViewModel
{
public OptionViewModel OptionViewModel { get; set; }
public TextBoxViewModel TextBoxViewModel { get; set; }
[font=Menlo] public static Func<FormViewModel> Create = ()
=> new FormViewModel()
{
OptionViewModel = new OptionViewModel(),
TextBoxViewModel = new TextBoxViewModel()
};[/font]
public static (OptionViewModel optVm, TextBoxViewModel txtBoxVm) CreateTuple ()
{
return (new OptionViewModel(), new TextBoxViewModel());
}
}
public class OptionViewModel { }
public class TextBoxViewModel { }
[[i] 本帖最後由 form5 於 2019-6-1 12:31 AM 編輯 [/i]]
form5 2019-6-1 00:27
[quote]原帖由 [i]form5[/i] 於 2019-6-1 12:25 AM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500257399&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
you can if you want that in C#
in dotnet core you don't have to add ValueTuple reference explicitly.
I prefer to return dummy class wrapper . class Program
{
static void Main( ... [/quote]
the code tag here really sucks
assembly.jc 2019-6-2 00:04
[quote]原帖由 [i]form5[/i] 於 2019-6-1 12:25 AM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500257399&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
you can if you want that in C#
in dotnet core you don't have to add ValueTuple reference explicitly.
I prefer to return dummy class wrapper . class Program
{
static void Main( ... [/quote]
點解 prefer to return dummy class?
Tuple 除咗可以裝多個唔同 type 嘅 Objects 之外,仲有 d syntax sugar 可以玩,如 swap variables 嘅值:
C style:
int a = 10, b = 15, tmp;
tmp = a;
a = b;
b = tmp;
Python style:
a = 10
b = 15
(a, b) = (b, a)
form5 2019-6-3 22:18
[quote]原帖由 [i]assembly.jc[/i] 於 2019-6-2 12:04 AM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500305758&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
點解 prefer to return dummy class?
Tuple 除咗可以裝多個唔同 type 嘅 Objects 之外,仲有 d syntax sugar 可以玩,如 swap variables 嘅值:
C style:
int a = 10, b = 15, tmp;
tmp = a;
a = b;
b = tmp;
Python style: ... [/quote]
因為係C# 可以bind 去 view model, 特別razor page,下下都auto bind 個 class property , 入面又隨時容易加helper function,c# Tuple呢 入面return 三個以上 obj, 新手所為。
Tuple 都可以甘用, 類似 go 甘,
(X xobj, bool ok) GetXDataFromDb( ....)
{
....
}
一係 全部 return value type Tuple, 大量計算會快
呢d Pattern 都幾乎日日用到,
Python Tuple swap 己經好耐冇用過肋
assembly.jc 2019-6-4 03:41
[quote]原帖由 [i]form5[/i] 於 2019-6-3 10:18 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500408193&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
因為係C# 可以bind 去 view model, 特別razor page,下下都auto bind 個 class property , 入面又隨時容易加helper function,c# Tuple呢 入面return 三個以上 obj, 新手所為。
Tuple 都可以甘用, 類似 go 甘,
(X xobj, bool ok) GetXDataFromDb( ....)
... [/quote]
呢個就係之前所講 7.0 嘅 ValueTuple?
(X xobj, bool ok) GetXDataFromDb( ....)
{
....
}
(X xobj, bool ok) 己經係一個典型的 Tuple (type safe)。最好係連 return type 都唔需要 specify:
GetXDataFromDb( ....) {
...
return(xobj, ok);
}
個 type compiler 可以 inference 到。
[[i] 本帖最後由 assembly.jc 於 2019-6-4 04:09 AM 編輯 [/i]]
form5 2019-6-5 23:22
[quote]原帖由 [i]assembly.jc[/i] 於 2019-6-4 03:41 AM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500418795&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
呢個就係之前所講 7.0 嘅 ValueTuple?
(X xobj, bool ok) GetXDataFromDb( ....)
{
....
}
(X xobj, bool ok) 己經係一個典型的 Tuple (type safe)。最好係連 return type 都唔需要 specify:
GetXDataFromDb( ....) {
...
return ... [/quote]
呢個係要declare return type
唔會特別選7.0, visual studio 2017, 2019 default stable c# 係7.3, 2019 vs 可以用到dotnet core 3.0 和 可以選 c# 8 beta,
仲有 compile 時 check 下 應吾應該用nullable, .....
[[i] 本帖最後由 form5 於 2019-6-5 11:26 PM 編輯 [/i]]
assembly.jc 2019-6-6 15:07
[quote]原帖由 [i]form5[/i] 於 2019-6-5 11:22 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500519463&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
呢個係要declare return type
唔會特別選7.0, visual studio 2017, 2019 default stable c# 係7.3, 2019 vs 可以用到dotnet core 3.0 和 可以選 c# 8 beta,
仲有 compile 時 check 下 應吾應該用nullable, ..... ... [/quote]
(X xobj, bool ok) 呢個已經係唔比係 null?
nullable 係另一個問題,不過完全唔用 null 係可以嘅,好似 F# 咁轉用 Option,不過 functional language 有佢獨特嘅結構去處理這些問題 (包括 exception),OOP 無這方面工具,寫起 code 上泥會麻煩 d。
form5 2019-6-6 23:29
[quote]原帖由 [i]assembly.jc[/i] 於 2019-6-6 03:07 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500546927&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
(X xobj, bool ok) 呢個已經係唔比係 null?
[/quote]
you can if you want
form5 2019-6-6 23:36
[quote]原帖由 [i]assembly.jc[/i] 於 2019-6-6 03:07 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500546927&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
nullable 係另一個問題,不過完全唔用 null 係可以嘅,好似 F# 咁轉用 Option,不過 functional language 有佢獨特嘅結構去處理這些問題 (包括 exception),OOP 無這方面工具,寫起 code 上泥會麻煩 d。 ... [/quote]
In C#, you may use the F# Option, Union Type, Function, Module ... seamlessly ....:lol, they are both same dotnet family
t_y_mak 2019-6-7 13:15
[quote]原帖由 [i]assembly.jc[/i] 於 2019-5-30 01:37 PM 發表 [url=https://www.discuss.com.hk/redirect.php?goto=findpost&pid=500177753&ptid=28250398][img]https://www.discuss.com.hk/images/common/back.gif[/img][/url]
一個簡單的例子:
In Java, 要回傳 2 個唔同型態的 Objects,可以用 Array,如: return(new Object[] {trainer, trainData}),但咁做,compile time check 唔到型態,而且要用個陣要 casting。好 d 嘅方法,好似以上個例子咁用 Generic Type。但如果回傳 3, 4, 5 ...個唔唔同型態的 Obj ... [/quote]
in java, I will create a class for the return.
assembly.jc 2019-6-7 17:15
[quote]原帖由 [i]form5[/i] 於 2019-6-6 11:36 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500569799&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
In C#, you may use the F# Option, Union Type, Function, Module ... seamlessly ....:lol, they are both same dotnet family [/quote]
seamlessly...?
用到 F# 嘅 Object,但用唔到 F# 嘅語法。舉個例子:
[url=https://fsharpforfunandprofit.com/posts/elevated-world-2/]https://fsharpforfunandprofit.com/posts/elevated-world-2/[/url]
F# 用 >>= 做 bind,Option 可以用以下方式去簡化 coding:
let parseOrderQty_alt str =
str |> parseInt >>= toOrderQty
C# 要寫成:
Option parseOrderQty(string str) {
return(Option.bind(toOrderQty, parseInt(str));
}
好似分別不大,但如果再多幾個 bind,如
expression1 >>= expression2 >>= expression3 >>= expression4
就會變成:
Option.bind(expression4, Option.bind(expression3, Option.bind(expression2, expression1(xxx))));
簡潔性和可讀性即時降低。
[[i] 本帖最後由 assembly.jc 於 2019-6-7 05:27 PM 編輯 [/i]]
assembly.jc 2019-6-7 17:26
條 link 仲提到,F# 只係借用左 Monad bind 嘅 concept,但 Option, List 並非 Monad。如果用練武功做比喻:
0. F# 只去到第 0 層
1. Monad 係第 1 層
2. Monad Transformers 係第 2 層
3. Free Monad Transformers: FreeT 係第 3 層: [url=https://stackoverflow.com/questions/53462008/zipping-free-monad-transformers]https://stackoverflow.com/questions/53462008/zipping-free-monad-transformers[/url]
...
一般人練到第 2 層就開始感到吃力,第 3 層要有天分先去到,之後更抽象嘅小弟暫未見識到。
assembly.jc 2019-6-7 17:31
[quote]原帖由 [i]t_y_mak[/i] 於 2019-6-7 01:15 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500589794&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
in java, I will create a class for the return. [/quote]
咁會多左好多 dummy class,因為只係為左裝住 2 個唔同 type 嘅 objects,無其他嘅 operations。
t_y_mak 2019-6-7 21:20
[quote]原帖由 [i]assembly.jc[/i] 於 2019-6-7 05:31 PM 發表 [url=https://www.discuss.com.hk/redirect.php?goto=findpost&pid=500599720&ptid=28250398][img]https://www.discuss.com.hk/images/common/back.gif[/img][/url]
咁會多左好多 dummy class,因為只係為左裝住 2 個唔同 type 嘅 objects,無其他嘅 operations。 [/quote]
我指係把要的results 放落一個 自己寫的class的object做return。
form5 2019-6-7 22:19
[quote]原帖由 [i]assembly.jc[/i] 於 2019-6-7 05:15 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500599050&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
seamlessly...?
用到 F# 嘅 Object,但用唔到 F# 嘅語法。舉個例子:
[url=https://fsharpforfunandprofit.com/posts/elevated-world-2/]https://fsharpforfunandprofit.com/posts/elevated-world-2/[/url]
F# 用 >>= 做 bind,Option 可以用以下方式去簡化 coding:
let parseOrderQty_alt str =
str |> parse ... [/quote]
my way in pure C#, if someone insist to use bind, this bind at least pass the "law of monad' unit test.:lol
form5 2019-6-7 22:38
[quote]原帖由 [i]assembly.jc[/i] 於 2019-6-7 05:26 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500599497&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
條 link 仲提到,F# 只係借用左 Monad bind 嘅 concept,但 Option, List 並非 Monad。如果用練武功做比喻:
0. F# 只去到第 0 層
1. Monad 係第 1 層
2. Monad Transformers 係第 2 層
3. Free Monad Transformers: FreeT 係第 3 層: https://stackover ... [/quote]
所以呢 ,Haskell 永遠都係學堂入面嘅小衆游戱,追求abstract syntax, 假如有一隻library 做到nodejs 功能,解決到最低B嘅問題,己經可以震驚世界了
assembly.jc 2019-6-8 18:18
[quote]原帖由 [i]form5[/i] 於 2019-6-7 10:38 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500613718&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
所以呢 ,Haskell 永遠都係學堂入面嘅小衆游戱,追求abstract syntax, 假如有一隻library 做到nodejs 功能,解決到最低B嘅問題,己經可以震驚世界了 [/quote]
其實又唔係好關 Haskell 事,Monad 嘅 concept 60 年代 Category Theory 已經有,92 年先比 Philip Wadler 引入 Functional Programming,用來解決 Pure 同 Impure 程式結合的問題。這些理論的發展主要來自 FP 的論文,Haskell 只是實踐這些理論的平台。
而且這些理論唔係一味追求 Abstraction,無左 Monad transformers,寫 Stateful 嘅 program 會麻煩好多,而 Free Monad transformers 解決左 Network IO 嘅問題。呢 d 之前小弟都開帖講過,唔再重覆了。
assembly.jc 2019-6-9 00:26
[quote]原帖由 [i]form5[/i] 於 2019-6-7 10:19 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500612700&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
my way in pure C#, if someone insist to use bind, this bind at least pass the "law of monad' unit test.:lol [/quote]
睇漏左... 原來 C# 有 d 咁嘅語法
Parse(s).Bind(OrderQty.of)
Bind 唔係有 2 個 parameters?
(.) operator 是 function composition 定係 call Object 的 method 即 Object.method(...)?
咁有無 Applicative functor 對應的語法 (因為 every Monad is an Applicative functor),例如:
Haskell: try it: [url=https://repl.it/repls/UpbeatUnwillingMacro]https://repl.it/repls/UpbeatUnwillingMacro[/url]
main = print $ parseSubTotal "10" "20"
subTotal qty price = qty * price
parse x = (readMaybe x::Maybe Int)
parseSubTotal qtyStr priceStr = subTotal <$> parse qtyStr <*> parse priceStr
F# 好似無 applicative functor,但好似有另外方法去做。
form5 2019-6-9 22:26
[quote]原帖由 [i]assembly.jc[/i] 於 2019-6-9 12:26 AM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500671368&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
睇漏左... 原來 C# 有 d 咁嘅語法
Parse(s).Bind(OrderQty.of)
Bind 唔係有 2 個 parameters?
(.) operator 是 function composition 定係 call Object 的 method 即 Object.method(...)?
咁有無 Applicative functor 對應的語法 ... [/quote]
you can if you want that in C#, and if you know C# very well
assembly.jc 2019-6-10 21:54
[quote]原帖由 [i]form5[/i] 於 2019-6-9 10:26 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500720705&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
you can if you want that in C#, and if you know C# very well [/quote]
小弟 C# 係有限工司,依家都未攪清楚 Bind(), Apply() 點運作,好似 Bind() 咁,它只接受一個 parameter 嗎? 如果係,運算中間要 extract Monad 個 value 出來,怎樣做?
即係,例如,用返之前嘅例子,計算 Order 嘅 sub-total,但今次多幾個 Items,Input 是 2 個 List 裝住:
priceIn = ["10", "20", "30"]
qtyIn = ["1", "2", "3"]
[b]係唔改 Parse method 之前提下:[/b]
Haskell do notation version:
subTotalDo priceIn qtyIn =
do prices <- sequence $ map parse priceIn
qtys <- sequence $ map parse qtyIn
return (sum $ zipWith (*) prices qtys)
Equivalently, using bind (>>=):
subTotalBind priceIn qtyIn =
(sequence $ map parse priceIn)
>>= (\prices-> (sequence $ map parse qtyIn)
>>= (\qtys-> return (sum $ zipWith (*) prices qtys)))
Or, Functional version:
subTotal prices qtys =
fmap (sum . (uncurry $ zipWith (*)))
(mzip (sequence $ map parse prices)
(sequence $ map parse qtys))
C# 可以模擬到邊個 version? do notation 最 concise and readable,但C# 應該無這語法。Functional version 就需要 uncurry,如果無,就要另外整多個 zipWith function 出來。餘下 bind version,如果 Bind() 只接受一個 parameter,應該點做?
[[i] 本帖最後由 assembly.jc 於 2023-10-22 03:32 編輯 [/i]]
form5 2019-6-11 00:08
readable ? not that sure :smile_47:
Some and Option are struct
You may see Apply, Match as extension methods for Option,
Zip and Sum are from Linq,
Func is C# keyword
[[i] 本帖最後由 form5 於 2019-6-11 12:16 AM 編輯 [/i]]
assembly.jc 2019-6-12 00:47
[quote]原帖由 [i]form5[/i] 於 2019-6-11 12:08 AM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500784353&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
readable ? not that sure :smile_47:
Some and Option are struct
You may see Apply, Match as extension methods for Option,
Zip and Sum are from Linq,
Func is C# keyword [/quote]
readable 只係指上個帖 3 個 solutions (Haskell) 中,do notation 比較 readable。
呢個都係 Applicative functor 的解法,5 兄你上個帖已經講過。
其實我想知係 Bind() 點 extract 個 Monadic value 出來再運算。
例如 假設 Parse 可以將 string[] 轉成 Option<int[]> :
prices=Parse(priceIn)
.Bind(qyts=Parse(qtyIn))
.Bind(calSubTotal(prices, qtys))
int calSubTotal(int[] prices, int[] qtys) {...}
當然這是假想的 coding,但大致係咁嘅意思,pirces, qtys 這些 extract 出來嘅 Monadic value 點樣係 Bind() 之間流動同點表達出來?
另外,如果,其中一個 qty 係 "b",subtotal return None instead of 140。
form5 2019-6-12 23:41
I try if you insist
assembly.jc 2019-6-13 17:39
[quote]原帖由 [i]form5[/i] 於 2019-6-12 11:41 PM 發表 [url=https://computer.discuss.com.hk/redirect.php?goto=findpost&pid=500905415&ptid=28250398][img]https://computer.discuss.com.hk/images/common/back.gif[/img][/url]
I try if you insist [/quote]
多謝你個答案先,我慳返唔少 google 嘅時間。小弟只是好奇,OOP 同 FP 二種截然不同處理 variable 嘅方式,點樣結合埋一齊。如果呢個係唯一使用 Bind() 嘅解,我估計 expression:
x => Int.Parse(Item.v1).Bind<Int, Int>(y=>x*y)
雖然夾雜住 OO 嘅 Object,但最終會當成 lambda expression 來處理,咁 x 就唔駛 declare 同個 scope 就完全唔係問題,因為跟返 FP 個套,x 只是 (y=>x*y) 一個 free variable 而已 (同理,Item.v1 也是 free variable 對於整條 expression 而言)。
算唔算結合左? 其實都算,至少 x, y 係 FP, immutable,Item.v1 係 OO,mutable,係同一條 expression 出現,但整條 expression 變左 Impure。
頁:
[1]