Last active
December 1, 2018 17:15
-
-
Save iwamoto-takaaki/8419a7609689695356be3abd59c4af95 to your computer and use it in GitHub Desktop.
RubyのTimesをC#の拡張メソッドで実装してみた ref: https://qiita.com/takaaki-iwamoto/items/7f59f9768d1e757e74a7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3.times{|i| | |
p i | |
} | |
# out => | |
# 0 | |
# 1 | |
# 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
public class Program{ | |
public static void Main(){ | |
Enumerable.Range(0,3).ToList().ForEach(i => | |
Console.WriteLine(i) | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Program{ | |
public static void Main(){ | |
// Enumerable.Range(0,3).ToList().ForEach(i => | |
for(var i = 0; i < 3; ++i){ | |
Console.WriteLine(i); | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class Program{ | |
public static void Main(){ | |
3.Times(i => | |
Console.Write(i) | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.upto(3).each{|i| | |
p i | |
} | |
# out => | |
# 1 | |
# 2 | |
# 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class Program{ | |
public static void Main(){ | |
1.To(3).ToList().ForEach(i => | |
Console.Write(i) | |
); | |
} | |
} | |
// out => | |
// 1 | |
// 2 | |
// 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class Program{ | |
public static void Main(){ | |
foreach(var i in 1.To(3)) { | |
Console.Write(i); | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
public static class Extension{ | |
public static IEnumerable<int> To(this int from, int to){ | |
return Enumerable.Range(from, to - from + 1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class Program{ | |
public static void Main(){ | |
1.To(3).ToList().ForEach(i => | |
Console.Write(i) | |
); | |
} | |
} | |
public static class Extension{ | |
public static IEnumerable<int> To(this int from, int to){ | |
return Enumerable.Range(from, to - from + 1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class Program{ | |
public static void Main(){ | |
3.Times(i => | |
Console.Write(i) | |
); | |
} | |
} | |
public static class Extension{ | |
public static IEnumerable<int> To(this int from, int to){ | |
return Enumerable.Range(from, to - from + 1); | |
} | |
// albireoさんの提案のコード追記 | |
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action){ | |
source.All(x => {action(x); return true;}); | |
} | |
public static void Times(this int times, Action<int> action){ | |
// 0.To(times - 1).ToList().ForEach(x => action(x)); | |
0.To(times - 1).ForEach(x => action(x)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment