Instantly share code, notes, and snippets.

View gist:694445f9c65bdc955c0c106d15c43138
### Keybase proof
I hereby claim:
* I am Hazealign on github.
* I am realignist (https://keybase.io/realignist) on keybase.
* I have a public key whose fingerprint is E5E3 E855 9C81 5BCE 7A87 D394 1802 F0A1 8A2F 28A8
To claim this, I am signing this object:
View Error Log (R Repl & g++-5.4.0)
> devtools::install_github("ohdsi/CohortMethod", ref = "v2.5.0")
Downloading GitHub repo ohdsi/CohortMethod@v2.5.0
from URL https://api.github.com/repos/ohdsi/CohortMethod/zipball/v2.5.0
Installing CohortMethod
--2018-04-10 02:58:17-- https://cran.rstudio.com/src/contrib/Cyclops_1.3.0.tar.gz
Resolving cran.rstudio.com (cran.rstudio.com)... 54.230.255.50
Connecting to cran.rstudio.com (cran.rstudio.com)|54.230.255.50|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 349068 (341K) [application/x-gzip]
Saving to: ‘/tmp/Rtmpsgbaaz/Cyclops_1.3.0.tar.gz’
View Snippet-14.cs
switch (o)
{
case int n when n > 0: return 1;
// 절대 매칭될 일이 없지만, 컴파일러는 이걸 당신에게 경고하지 않을 것입니다.
case int n when n > 1: return 2;
}
View Snippet-13.cs
switch (o)
{
case int n: return 1;
// 에러: 이 분기는 이미 이전 case에서 처리되었습니다.
case int n when n == 1: return 2;
}
View Snippet-12.cs
if (o is int n && n == 1) return 1;
if (o is string s && s == "") return 2;
if (o is int n2 && n2 == 2) return 3;
return -1;
View Snippet-11.cs
switch (o)
{
// 만약 o가 int일 경우 최소한 하나 이상의 타입 검사와 언박싱이 들어가서
// 생성된 코드는 최적화가 덜 될 것입니다.
case int n when n == 1: return 1;
case string s when s == "": return 2;
case int n when n == 2: return 3;
default: return -1;
}
View Snippet-10.cs
public static void FizzBuzz(object o)
{
// 모든 경우는 값이 null이 아닐 때만 매칭할 수 있습니다.
if (o != null)
{
if (o is string s &&
(s.Contains("Fizz") || s.Contains("Buzz")))
{
Console.WriteLine(s);
return;
View Snippet-09.cs
public static void FizzBuzz(object o)
{
switch (o)
{
case string s when s.Contains("Fizz") || s.Contains("Buzz"):
Console.WriteLine(s);
break;
case int n when n % 5 == 0 && n % 3 == 0:
Console.WriteLine("FizzBuzz");
break;
View Snippet-08.cs
public static int Count<T>(this IEnumerable<T> e)
{
switch (e)
{
case ICollection<T> c: return c.Count;
case IReadOnlyCollection<T> c: return c.Count;
// concurrent collection일 때 매칭합니다.
case IProducerConsumerCollection<T> pc: return pc.Count;
// e가 null이 아닐 때 매칭합니다.
case IEnumerable<T> _: return e.Count();
View Snippet-07.cs
public void IsVar(object o)
{
if (o is var x) Console.WriteLine($"x: {x}");
}