├── .idea └── .idea.js-ts-csharp.dir │ └── .idea │ ├── .gitignore │ ├── aws.xml │ ├── indexLayout.xml │ ├── misc.xml │ └── vcs.xml ├── .vscode ├── launch.json └── tasks.json ├── README.md ├── cs-battleships ├── .gitignore ├── Program.cs └── cs-battleships.csproj ├── cs-functional ├── .gitignore ├── Program.cs └── cs-functional.csproj ├── cs ├── .gitignore ├── Program.cs └── cs.csproj ├── js-battleships └── battleships.js ├── js-ts-csharp.png ├── js-ts-csharp.sln ├── js └── sample.js ├── ts-battleships ├── battleships.js ├── battleships.ts └── tsconfig.json └── ts ├── sample.js ├── sample.ts └── tsconfig.json /.idea/.idea.js-ts-csharp.dir/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Rider ignored files 5 | /modules.xml 6 | /projectSettingsUpdater.xml 7 | /contentModel.xml 8 | /.idea.js-ts-csharp.iml 9 | # Editor-based HTTP Client requests 10 | /httpRequests/ 11 | # Datasource local storage ignored files 12 | /dataSources/ 13 | /dataSources.local.xml 14 | -------------------------------------------------------------------------------- /.idea/.idea.js-ts-csharp.dir/.idea/aws.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 16 | 17 | -------------------------------------------------------------------------------- /.idea/.idea.js-ts-csharp.dir/.idea/indexLayout.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/.idea.js-ts-csharp.dir/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /.idea/.idea.js-ts-csharp.dir/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | // Use IntelliSense to find out which attributes exist for C# debugging 6 | // Use hover for the description of the existing attributes 7 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md 8 | "name": ".NET Core Launch (console)", 9 | "type": "coreclr", 10 | "request": "launch", 11 | "preLaunchTask": "build", 12 | // If you have changed target frameworks, make sure to update the program path. 13 | "program": "${workspaceFolder}/cs/bin/Debug/net6.0/cs.dll", 14 | "args": [], 15 | "cwd": "${workspaceFolder}/cs", 16 | // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console 17 | "console": "internalConsole", 18 | "stopAtEntry": false 19 | }, 20 | { 21 | "name": ".NET Core Attach", 22 | "type": "coreclr", 23 | "request": "attach" 24 | } 25 | ] 26 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "label": "build", 6 | "command": "dotnet", 7 | "type": "process", 8 | "args": [ 9 | "build", 10 | "${workspaceFolder}/cs/cs.csproj", 11 | "/property:GenerateFullPaths=true", 12 | "/consoleloggerparameters:NoSummary" 13 | ], 14 | "problemMatcher": "$msCompile" 15 | }, 16 | { 17 | "label": "publish", 18 | "command": "dotnet", 19 | "type": "process", 20 | "args": [ 21 | "publish", 22 | "${workspaceFolder}/cs/cs.csproj", 23 | "/property:GenerateFullPaths=true", 24 | "/consoleloggerparameters:NoSummary" 25 | ], 26 | "problemMatcher": "$msCompile" 27 | }, 28 | { 29 | "label": "watch", 30 | "command": "dotnet", 31 | "type": "process", 32 | "args": [ 33 | "watch", 34 | "run", 35 | "--project", 36 | "${workspaceFolder}/cs/cs.csproj" 37 | ], 38 | "problemMatcher": "$msCompile" 39 | } 40 | ] 41 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Building up from JavaScript to TypeScript to C# 10 and .NET 6 2 | 3 | > 👋 Hey there, if you're interested in learning C# and you already know TypeScript or vice versa, check out [TypeScript is Like C#](https://typescript-is-like-csharp.chrlschn.dev/). It's an extension of this that goes deeper into the similarities between TypeScript and C#! 4 | 5 | This repository is meant to highlight some of the various functional techniques available in C#. 6 | 7 | Read more here: https://chrlschn.medium.com/building-up-from-javascript-to-typescript-to-c-10-and-net-6-669a70cd0a66 8 | 9 | ![JS vs TS vs CS](./js-ts-csharp.png) 10 | 11 | These functional elements of C# ultimately mean that there is quite a bit of syntactic congruence with JavaScript and TypeScript; in fact, I first noticed that C# and JavaScript starting to converge around the release of .NET 3.0. 12 | 13 | For many developers that are ready to extend from JavaScript and TypeScript on the backend to a more secure, performant, and robust backend runtime, C# on .NET is a natural extension as it has a clear lineage with JavaScript and TypeScript while providing many benefits including easy mutli-threading, language integrated query (LINQ), and many other features. 14 | 15 | If you'd like to learn more about C#'s functional features, check out: 16 | 17 | - [Lambda Expressions](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions) 18 | - [Local Functions](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions) 19 | - [Pattern Matching](https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching) 20 | - [Discards](https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/discards) 21 | - [Desconstructing](https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/deconstruct) 22 | - [var and Implicit Typing](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var) 23 | - [Object Initializers](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-initialize-objects-by-using-an-object-initializer) 24 | - [Array Initializers](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/single-dimensional-arrays) 25 | 26 | ## If You'd Like to Contribute... 27 | 28 | Make a fork and send a PR! I'd love to see folks add more examples. 29 | 30 | (It may make sense to have sub-folders under `ts`, `js`, and `csharp`) 31 | 32 | ## Running the JavaScript Sample 33 | 34 | Install Node: https://nodejs.org/en/download/ 35 | 36 | To run the JavaScript sample: 37 | 38 | ``` 39 | cd js 40 | node sample.js 41 | ``` 42 | 43 | To try the battleships sample: 44 | 45 | ``` 46 | cd js-battleships 47 | node battleships.js 48 | ``` 49 | 50 | ## Running the TypeScript Sample 51 | 52 | Install TypeScript: https://www.typescriptlang.org/download 53 | 54 | To run the TypeScript sample: 55 | 56 | ``` 57 | cd ts 58 | tsc 59 | node sample.js 60 | ``` 61 | 62 | To try the battleships sample: 63 | 64 | ``` 65 | cd ts-battleships 66 | tsc 67 | node battleships.js 68 | ``` 69 | 70 | ## Running the C# Sample 71 | 72 | Install the .NET SDK: https://dotnet.microsoft.com/en-us/download 73 | 74 | To run the base C# sample: 75 | 76 | ``` 77 | cd cs 78 | dotnet run 79 | ``` 80 | 81 | You can also try out the Battleships C# sample which is even more functional and demonstrates recursion with C# local functions: 82 | 83 | ``` 84 | cd cs-battleships 85 | dotnet run 86 | ``` 87 | 88 | To create your own .NET console project once you have the SDK installed: 89 | 90 | ``` 91 | dotnet new console 92 | ``` 93 | -------------------------------------------------------------------------------- /cs-battleships/.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # Tye 66 | .tye/ 67 | 68 | # ASP.NET Scaffolding 69 | ScaffoldingReadMe.txt 70 | 71 | # StyleCop 72 | StyleCopReport.xml 73 | 74 | # Files built by Visual Studio 75 | *_i.c 76 | *_p.c 77 | *_h.h 78 | *.ilk 79 | *.meta 80 | *.obj 81 | *.iobj 82 | *.pch 83 | *.pdb 84 | *.ipdb 85 | *.pgc 86 | *.pgd 87 | *.rsp 88 | *.sbr 89 | *.tlb 90 | *.tli 91 | *.tlh 92 | *.tmp 93 | *.tmp_proj 94 | *_wpftmp.csproj 95 | *.log 96 | *.vspscc 97 | *.vssscc 98 | .builds 99 | *.pidb 100 | *.svclog 101 | *.scc 102 | 103 | # Chutzpah Test files 104 | _Chutzpah* 105 | 106 | # Visual C++ cache files 107 | ipch/ 108 | *.aps 109 | *.ncb 110 | *.opendb 111 | *.opensdf 112 | *.sdf 113 | *.cachefile 114 | *.VC.db 115 | *.VC.VC.opendb 116 | 117 | # Visual Studio profiler 118 | *.psess 119 | *.vsp 120 | *.vspx 121 | *.sap 122 | 123 | # Visual Studio Trace Files 124 | *.e2e 125 | 126 | # TFS 2012 Local Workspace 127 | $tf/ 128 | 129 | # Guidance Automation Toolkit 130 | *.gpState 131 | 132 | # ReSharper is a .NET coding add-in 133 | _ReSharper*/ 134 | *.[Rr]e[Ss]harper 135 | *.DotSettings.user 136 | 137 | # TeamCity is a build add-in 138 | _TeamCity* 139 | 140 | # DotCover is a Code Coverage Tool 141 | *.dotCover 142 | 143 | # AxoCover is a Code Coverage Tool 144 | .axoCover/* 145 | !.axoCover/settings.json 146 | 147 | # Coverlet is a free, cross platform Code Coverage Tool 148 | coverage*.json 149 | coverage*.xml 150 | coverage*.info 151 | 152 | # Visual Studio code coverage results 153 | *.coverage 154 | *.coveragexml 155 | 156 | # NCrunch 157 | _NCrunch_* 158 | .*crunch*.local.xml 159 | nCrunchTemp_* 160 | 161 | # MightyMoose 162 | *.mm.* 163 | AutoTest.Net/ 164 | 165 | # Web workbench (sass) 166 | .sass-cache/ 167 | 168 | # Installshield output folder 169 | [Ee]xpress/ 170 | 171 | # DocProject is a documentation generator add-in 172 | DocProject/buildhelp/ 173 | DocProject/Help/*.HxT 174 | DocProject/Help/*.HxC 175 | DocProject/Help/*.hhc 176 | DocProject/Help/*.hhk 177 | DocProject/Help/*.hhp 178 | DocProject/Help/Html2 179 | DocProject/Help/html 180 | 181 | # Click-Once directory 182 | publish/ 183 | 184 | # Publish Web Output 185 | *.[Pp]ublish.xml 186 | *.azurePubxml 187 | # Note: Comment the next line if you want to checkin your web deploy settings, 188 | # but database connection strings (with potential passwords) will be unencrypted 189 | *.pubxml 190 | *.publishproj 191 | 192 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 193 | # checkin your Azure Web App publish settings, but sensitive information contained 194 | # in these scripts will be unencrypted 195 | PublishScripts/ 196 | 197 | # NuGet Packages 198 | *.nupkg 199 | # NuGet Symbol Packages 200 | *.snupkg 201 | # The packages folder can be ignored because of Package Restore 202 | **/[Pp]ackages/* 203 | # except build/, which is used as an MSBuild target. 204 | !**/[Pp]ackages/build/ 205 | # Uncomment if necessary however generally it will be regenerated when needed 206 | #!**/[Pp]ackages/repositories.config 207 | # NuGet v3's project.json files produces more ignorable files 208 | *.nuget.props 209 | *.nuget.targets 210 | 211 | # Microsoft Azure Build Output 212 | csx/ 213 | *.build.csdef 214 | 215 | # Microsoft Azure Emulator 216 | ecf/ 217 | rcf/ 218 | 219 | # Windows Store app package directories and files 220 | AppPackages/ 221 | BundleArtifacts/ 222 | Package.StoreAssociation.xml 223 | _pkginfo.txt 224 | *.appx 225 | *.appxbundle 226 | *.appxupload 227 | 228 | # Visual Studio cache files 229 | # files ending in .cache can be ignored 230 | *.[Cc]ache 231 | # but keep track of directories ending in .cache 232 | !?*.[Cc]ache/ 233 | 234 | # Others 235 | ClientBin/ 236 | ~$* 237 | *~ 238 | *.dbmdl 239 | *.dbproj.schemaview 240 | *.jfm 241 | *.pfx 242 | *.publishsettings 243 | orleans.codegen.cs 244 | 245 | # Including strong name files can present a security risk 246 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 247 | #*.snk 248 | 249 | # Since there are multiple workflows, uncomment next line to ignore bower_components 250 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 251 | #bower_components/ 252 | 253 | # RIA/Silverlight projects 254 | Generated_Code/ 255 | 256 | # Backup & report files from converting an old project file 257 | # to a newer Visual Studio version. Backup files are not needed, 258 | # because we have git ;-) 259 | _UpgradeReport_Files/ 260 | Backup*/ 261 | UpgradeLog*.XML 262 | UpgradeLog*.htm 263 | ServiceFabricBackup/ 264 | *.rptproj.bak 265 | 266 | # SQL Server files 267 | *.mdf 268 | *.ldf 269 | *.ndf 270 | 271 | # Business Intelligence projects 272 | *.rdl.data 273 | *.bim.layout 274 | *.bim_*.settings 275 | *.rptproj.rsuser 276 | *- [Bb]ackup.rdl 277 | *- [Bb]ackup ([0-9]).rdl 278 | *- [Bb]ackup ([0-9][0-9]).rdl 279 | 280 | # Microsoft Fakes 281 | FakesAssemblies/ 282 | 283 | # GhostDoc plugin setting file 284 | *.GhostDoc.xml 285 | 286 | # Node.js Tools for Visual Studio 287 | .ntvs_analysis.dat 288 | node_modules/ 289 | 290 | # Visual Studio 6 build log 291 | *.plg 292 | 293 | # Visual Studio 6 workspace options file 294 | *.opt 295 | 296 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 297 | *.vbw 298 | 299 | # Visual Studio LightSwitch build output 300 | **/*.HTMLClient/GeneratedArtifacts 301 | **/*.DesktopClient/GeneratedArtifacts 302 | **/*.DesktopClient/ModelManifest.xml 303 | **/*.Server/GeneratedArtifacts 304 | **/*.Server/ModelManifest.xml 305 | _Pvt_Extensions 306 | 307 | # Paket dependency manager 308 | .paket/paket.exe 309 | paket-files/ 310 | 311 | # FAKE - F# Make 312 | .fake/ 313 | 314 | # CodeRush personal settings 315 | .cr/personal 316 | 317 | # Python Tools for Visual Studio (PTVS) 318 | __pycache__/ 319 | *.pyc 320 | 321 | # Cake - Uncomment if you are using it 322 | # tools/** 323 | # !tools/packages.config 324 | 325 | # Tabs Studio 326 | *.tss 327 | 328 | # Telerik's JustMock configuration file 329 | *.jmconfig 330 | 331 | # BizTalk build output 332 | *.btp.cs 333 | *.btm.cs 334 | *.odx.cs 335 | *.xsd.cs 336 | 337 | # OpenCover UI analysis results 338 | OpenCover/ 339 | 340 | # Azure Stream Analytics local run output 341 | ASALocalRun/ 342 | 343 | # MSBuild Binary and Structured Log 344 | *.binlog 345 | 346 | # NVidia Nsight GPU debugger configuration file 347 | *.nvuser 348 | 349 | # MFractors (Xamarin productivity tool) working folder 350 | .mfractor/ 351 | 352 | # Local History for Visual Studio 353 | .localhistory/ 354 | 355 | # BeatPulse healthcheck temp database 356 | healthchecksdb 357 | 358 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 359 | MigrationBackup/ 360 | 361 | # Ionide (cross platform F# VS Code tools) working folder 362 | .ionide/ 363 | 364 | # Fody - auto-generated XML schema 365 | FodyWeavers.xsd 366 | 367 | ## 368 | ## Visual studio for Mac 369 | ## 370 | 371 | 372 | # globs 373 | Makefile.in 374 | *.userprefs 375 | *.usertasks 376 | config.make 377 | config.status 378 | aclocal.m4 379 | install-sh 380 | autom4te.cache/ 381 | *.tar.gz 382 | tarballs/ 383 | test-results/ 384 | 385 | # Mac bundle stuff 386 | *.dmg 387 | *.app 388 | 389 | # content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore 390 | # General 391 | .DS_Store 392 | .AppleDouble 393 | .LSOverride 394 | 395 | # Icon must end with two \r 396 | Icon 397 | 398 | 399 | # Thumbnails 400 | ._* 401 | 402 | # Files that might appear in the root of a volume 403 | .DocumentRevisions-V100 404 | .fseventsd 405 | .Spotlight-V100 406 | .TemporaryItems 407 | .Trashes 408 | .VolumeIcon.icns 409 | .com.apple.timemachine.donotpresent 410 | 411 | # Directories potentially created on remote AFP share 412 | .AppleDB 413 | .AppleDesktop 414 | Network Trash Folder 415 | Temporary Items 416 | .apdisk 417 | 418 | # content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore 419 | # Windows thumbnail cache files 420 | Thumbs.db 421 | ehthumbs.db 422 | ehthumbs_vista.db 423 | 424 | # Dump file 425 | *.stackdump 426 | 427 | # Folder config file 428 | [Dd]esktop.ini 429 | 430 | # Recycle Bin used on file shares 431 | $RECYCLE.BIN/ 432 | 433 | # Windows Installer files 434 | *.cab 435 | *.msi 436 | *.msix 437 | *.msm 438 | *.msp 439 | 440 | # Windows shortcuts 441 | *.lnk 442 | 443 | # JetBrains Rider 444 | .idea/ 445 | *.sln.iml 446 | 447 | ## 448 | ## Visual Studio Code 449 | ## 450 | .vscode/* 451 | !.vscode/settings.json 452 | !.vscode/tasks.json 453 | !.vscode/launch.json 454 | !.vscode/extensions.json 455 | -------------------------------------------------------------------------------- /cs-battleships/Program.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * The battleships problem requires counting the number of battleships 3 | * on a board. See boards at the end for examples. 4 | * 5 | * - The smallest battleship has size 1. 6 | * - Battleships can only be horizontal or vertical 7 | * - Battleships do not touch 8 | * 9 | * This solution demonstrates an approach to solving it using a recursive 10 | * local function. 11 | */ 12 | 13 | // Alias to simplify usage. 14 | var log = (object message) => Console.WriteLine(message); 15 | 16 | var TestCountShips = int (int[,] board) => { 17 | var width = board.GetLength(0); 18 | var height = board.GetLength(1); 19 | 20 | log($"Board is {width} by {height}"); 21 | 22 | Func? CountShips = null; 23 | 24 | // Local function within lambda function we use for recursion 25 | CountShips = (int x, int y, int total) => { 26 | if (x > width - 1 && y < height) { 27 | x = 0; 28 | y++; 29 | } 30 | 31 | if (y > height - 1) { 32 | return total; 33 | } 34 | 35 | if (board[x, y] == 1) { 36 | board[x, y] = 0; 37 | total++; 38 | 39 | for (int i = x + 1; i < width; i++) { 40 | if (board[i, y] == 0) { 41 | x = i - 1; // Skip ahead? 42 | break; 43 | } 44 | 45 | board[i, y] = 0; 46 | } 47 | 48 | for (int i = y + 1; i < height; i++) { 49 | if (board[x, i] == 0) { 50 | break; 51 | } 52 | board[x, i] = 0; 53 | } 54 | } 55 | 56 | // We found a zero; keep going across. 57 | return CountShips!(x + 1, y, total); 58 | }; 59 | 60 | return CountShips(0, 0, 0); 61 | }; 62 | 63 | // Test function 64 | var Expect = (int[,] board, int expected) => { 65 | var actual = TestCountShips(board); 66 | 67 | if(actual != expected) { 68 | log($"Expected {expected} but got {actual}"); 69 | } 70 | else { 71 | log($"Passed with {expected}."); 72 | } 73 | }; 74 | 75 | // Sample boards 76 | var board1 = new int[4,4] { 77 | {1,1,1,0}, 78 | {0,0,0,1}, 79 | {1,0,0,1}, 80 | {1,0,0,0}, 81 | }; 82 | 83 | Expect(board1, 3); 84 | 85 | var board2 = new int[5,5] { 86 | {1,1,1,0,1}, 87 | {0,0,0,1,0}, 88 | {1,0,0,1,0}, 89 | {1,0,0,0,0}, 90 | {0,0,0,1,1} 91 | }; 92 | 93 | Expect(board2, 5); 94 | 95 | var board3 = new int[5,5] { 96 | {1,0,1,0,1}, 97 | {0,1,0,1,0}, 98 | {1,0,1,0,1}, 99 | {0,1,0,1,0}, 100 | {1,0,1,0,1} 101 | }; 102 | 103 | Expect(board3, 13); 104 | -------------------------------------------------------------------------------- /cs-battleships/cs-battleships.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | cs_battleships 7 | enable 8 | enable 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /cs-functional/.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # Tye 66 | .tye/ 67 | 68 | # ASP.NET Scaffolding 69 | ScaffoldingReadMe.txt 70 | 71 | # StyleCop 72 | StyleCopReport.xml 73 | 74 | # Files built by Visual Studio 75 | *_i.c 76 | *_p.c 77 | *_h.h 78 | *.ilk 79 | *.meta 80 | *.obj 81 | *.iobj 82 | *.pch 83 | *.pdb 84 | *.ipdb 85 | *.pgc 86 | *.pgd 87 | *.rsp 88 | *.sbr 89 | *.tlb 90 | *.tli 91 | *.tlh 92 | *.tmp 93 | *.tmp_proj 94 | *_wpftmp.csproj 95 | *.log 96 | *.vspscc 97 | *.vssscc 98 | .builds 99 | *.pidb 100 | *.svclog 101 | *.scc 102 | 103 | # Chutzpah Test files 104 | _Chutzpah* 105 | 106 | # Visual C++ cache files 107 | ipch/ 108 | *.aps 109 | *.ncb 110 | *.opendb 111 | *.opensdf 112 | *.sdf 113 | *.cachefile 114 | *.VC.db 115 | *.VC.VC.opendb 116 | 117 | # Visual Studio profiler 118 | *.psess 119 | *.vsp 120 | *.vspx 121 | *.sap 122 | 123 | # Visual Studio Trace Files 124 | *.e2e 125 | 126 | # TFS 2012 Local Workspace 127 | $tf/ 128 | 129 | # Guidance Automation Toolkit 130 | *.gpState 131 | 132 | # ReSharper is a .NET coding add-in 133 | _ReSharper*/ 134 | *.[Rr]e[Ss]harper 135 | *.DotSettings.user 136 | 137 | # TeamCity is a build add-in 138 | _TeamCity* 139 | 140 | # DotCover is a Code Coverage Tool 141 | *.dotCover 142 | 143 | # AxoCover is a Code Coverage Tool 144 | .axoCover/* 145 | !.axoCover/settings.json 146 | 147 | # Coverlet is a free, cross platform Code Coverage Tool 148 | coverage*.json 149 | coverage*.xml 150 | coverage*.info 151 | 152 | # Visual Studio code coverage results 153 | *.coverage 154 | *.coveragexml 155 | 156 | # NCrunch 157 | _NCrunch_* 158 | .*crunch*.local.xml 159 | nCrunchTemp_* 160 | 161 | # MightyMoose 162 | *.mm.* 163 | AutoTest.Net/ 164 | 165 | # Web workbench (sass) 166 | .sass-cache/ 167 | 168 | # Installshield output folder 169 | [Ee]xpress/ 170 | 171 | # DocProject is a documentation generator add-in 172 | DocProject/buildhelp/ 173 | DocProject/Help/*.HxT 174 | DocProject/Help/*.HxC 175 | DocProject/Help/*.hhc 176 | DocProject/Help/*.hhk 177 | DocProject/Help/*.hhp 178 | DocProject/Help/Html2 179 | DocProject/Help/html 180 | 181 | # Click-Once directory 182 | publish/ 183 | 184 | # Publish Web Output 185 | *.[Pp]ublish.xml 186 | *.azurePubxml 187 | # Note: Comment the next line if you want to checkin your web deploy settings, 188 | # but database connection strings (with potential passwords) will be unencrypted 189 | *.pubxml 190 | *.publishproj 191 | 192 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 193 | # checkin your Azure Web App publish settings, but sensitive information contained 194 | # in these scripts will be unencrypted 195 | PublishScripts/ 196 | 197 | # NuGet Packages 198 | *.nupkg 199 | # NuGet Symbol Packages 200 | *.snupkg 201 | # The packages folder can be ignored because of Package Restore 202 | **/[Pp]ackages/* 203 | # except build/, which is used as an MSBuild target. 204 | !**/[Pp]ackages/build/ 205 | # Uncomment if necessary however generally it will be regenerated when needed 206 | #!**/[Pp]ackages/repositories.config 207 | # NuGet v3's project.json files produces more ignorable files 208 | *.nuget.props 209 | *.nuget.targets 210 | 211 | # Microsoft Azure Build Output 212 | csx/ 213 | *.build.csdef 214 | 215 | # Microsoft Azure Emulator 216 | ecf/ 217 | rcf/ 218 | 219 | # Windows Store app package directories and files 220 | AppPackages/ 221 | BundleArtifacts/ 222 | Package.StoreAssociation.xml 223 | _pkginfo.txt 224 | *.appx 225 | *.appxbundle 226 | *.appxupload 227 | 228 | # Visual Studio cache files 229 | # files ending in .cache can be ignored 230 | *.[Cc]ache 231 | # but keep track of directories ending in .cache 232 | !?*.[Cc]ache/ 233 | 234 | # Others 235 | ClientBin/ 236 | ~$* 237 | *~ 238 | *.dbmdl 239 | *.dbproj.schemaview 240 | *.jfm 241 | *.pfx 242 | *.publishsettings 243 | orleans.codegen.cs 244 | 245 | # Including strong name files can present a security risk 246 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 247 | #*.snk 248 | 249 | # Since there are multiple workflows, uncomment next line to ignore bower_components 250 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 251 | #bower_components/ 252 | 253 | # RIA/Silverlight projects 254 | Generated_Code/ 255 | 256 | # Backup & report files from converting an old project file 257 | # to a newer Visual Studio version. Backup files are not needed, 258 | # because we have git ;-) 259 | _UpgradeReport_Files/ 260 | Backup*/ 261 | UpgradeLog*.XML 262 | UpgradeLog*.htm 263 | ServiceFabricBackup/ 264 | *.rptproj.bak 265 | 266 | # SQL Server files 267 | *.mdf 268 | *.ldf 269 | *.ndf 270 | 271 | # Business Intelligence projects 272 | *.rdl.data 273 | *.bim.layout 274 | *.bim_*.settings 275 | *.rptproj.rsuser 276 | *- [Bb]ackup.rdl 277 | *- [Bb]ackup ([0-9]).rdl 278 | *- [Bb]ackup ([0-9][0-9]).rdl 279 | 280 | # Microsoft Fakes 281 | FakesAssemblies/ 282 | 283 | # GhostDoc plugin setting file 284 | *.GhostDoc.xml 285 | 286 | # Node.js Tools for Visual Studio 287 | .ntvs_analysis.dat 288 | node_modules/ 289 | 290 | # Visual Studio 6 build log 291 | *.plg 292 | 293 | # Visual Studio 6 workspace options file 294 | *.opt 295 | 296 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 297 | *.vbw 298 | 299 | # Visual Studio LightSwitch build output 300 | **/*.HTMLClient/GeneratedArtifacts 301 | **/*.DesktopClient/GeneratedArtifacts 302 | **/*.DesktopClient/ModelManifest.xml 303 | **/*.Server/GeneratedArtifacts 304 | **/*.Server/ModelManifest.xml 305 | _Pvt_Extensions 306 | 307 | # Paket dependency manager 308 | .paket/paket.exe 309 | paket-files/ 310 | 311 | # FAKE - F# Make 312 | .fake/ 313 | 314 | # CodeRush personal settings 315 | .cr/personal 316 | 317 | # Python Tools for Visual Studio (PTVS) 318 | __pycache__/ 319 | *.pyc 320 | 321 | # Cake - Uncomment if you are using it 322 | # tools/** 323 | # !tools/packages.config 324 | 325 | # Tabs Studio 326 | *.tss 327 | 328 | # Telerik's JustMock configuration file 329 | *.jmconfig 330 | 331 | # BizTalk build output 332 | *.btp.cs 333 | *.btm.cs 334 | *.odx.cs 335 | *.xsd.cs 336 | 337 | # OpenCover UI analysis results 338 | OpenCover/ 339 | 340 | # Azure Stream Analytics local run output 341 | ASALocalRun/ 342 | 343 | # MSBuild Binary and Structured Log 344 | *.binlog 345 | 346 | # NVidia Nsight GPU debugger configuration file 347 | *.nvuser 348 | 349 | # MFractors (Xamarin productivity tool) working folder 350 | .mfractor/ 351 | 352 | # Local History for Visual Studio 353 | .localhistory/ 354 | 355 | # BeatPulse healthcheck temp database 356 | healthchecksdb 357 | 358 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 359 | MigrationBackup/ 360 | 361 | # Ionide (cross platform F# VS Code tools) working folder 362 | .ionide/ 363 | 364 | # Fody - auto-generated XML schema 365 | FodyWeavers.xsd 366 | 367 | ## 368 | ## Visual studio for Mac 369 | ## 370 | 371 | 372 | # globs 373 | Makefile.in 374 | *.userprefs 375 | *.usertasks 376 | config.make 377 | config.status 378 | aclocal.m4 379 | install-sh 380 | autom4te.cache/ 381 | *.tar.gz 382 | tarballs/ 383 | test-results/ 384 | 385 | # Mac bundle stuff 386 | *.dmg 387 | *.app 388 | 389 | # content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore 390 | # General 391 | .DS_Store 392 | .AppleDouble 393 | .LSOverride 394 | 395 | # Icon must end with two \r 396 | Icon 397 | 398 | 399 | # Thumbnails 400 | ._* 401 | 402 | # Files that might appear in the root of a volume 403 | .DocumentRevisions-V100 404 | .fseventsd 405 | .Spotlight-V100 406 | .TemporaryItems 407 | .Trashes 408 | .VolumeIcon.icns 409 | .com.apple.timemachine.donotpresent 410 | 411 | # Directories potentially created on remote AFP share 412 | .AppleDB 413 | .AppleDesktop 414 | Network Trash Folder 415 | Temporary Items 416 | .apdisk 417 | 418 | # content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore 419 | # Windows thumbnail cache files 420 | Thumbs.db 421 | ehthumbs.db 422 | ehthumbs_vista.db 423 | 424 | # Dump file 425 | *.stackdump 426 | 427 | # Folder config file 428 | [Dd]esktop.ini 429 | 430 | # Recycle Bin used on file shares 431 | $RECYCLE.BIN/ 432 | 433 | # Windows Installer files 434 | *.cab 435 | *.msi 436 | *.msix 437 | *.msm 438 | *.msp 439 | 440 | # Windows shortcuts 441 | *.lnk 442 | 443 | # JetBrains Rider 444 | .idea/ 445 | *.sln.iml 446 | 447 | ## 448 | ## Visual Studio Code 449 | ## 450 | .vscode/* 451 | !.vscode/settings.json 452 | !.vscode/tasks.json 453 | !.vscode/launch.json 454 | !.vscode/extensions.json 455 | -------------------------------------------------------------------------------- /cs-functional/Program.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | // This listing shows some of the functional ways in which C# can be used. 3 | // use dotnet watch to play around with it. 4 | 5 | // Alias Console.WriteLine 6 | var log = (object msg) => Console.WriteLine(msg); 7 | var header = (string msg) => log($@" 8 | -------------- 9 | {msg} 10 | --------------"); 11 | 12 | // Start our examples! 13 | header("A list of functions that we can execute:"); 14 | var functions = new [] { 15 | (int x, int y) => x * y, 16 | (int x, int y) => x + y, 17 | (int x, int y) => x - y, 18 | (int x, int y) => x / y 19 | }; 20 | 21 | var x = 10; 22 | var y = 5; 23 | 24 | foreach (var fn in functions) { 25 | log(fn(x, y)); 26 | } 27 | 28 | header("The functions can also be named:"); 29 | var namedFunctions = new Dictionary>() { 30 | ["multiply"] = (int x, int y) => x * y, 31 | ["add"] = (int x, int y) => x + y, 32 | ["subtract"] = (int x, int y) => x - y, 33 | ["divide"] = (int x, int y) => x / y, 34 | }; 35 | 36 | log(namedFunctions["add"](x, y)); 37 | 38 | header("We can use references to the functions:"); 39 | var multiply = (int x, int y) => x * y; 40 | var add = (int x, int y) => x * y; 41 | var subtract = (int x, int y) => x - y; 42 | var divide = (int x, int y) => x / y; 43 | 44 | var namedFunctionsList = new [] { 45 | multiply, 46 | add, 47 | subtract, 48 | divide 49 | }; 50 | 51 | foreach (var fn in namedFunctionsList) { 52 | log(fn(x, y)); 53 | } 54 | 55 | header("We can also accept an arbitrary number of parameters:"); 56 | var multiplyN = (int[] numbers) => numbers.Aggregate(1, (a, b) => a * b); 57 | var addN = (int[] numbers) => numbers.Aggregate(0, (a, b) => a + b); 58 | var subtractN = (int[] numbers) => numbers.Aggregate(0, (a, b) => a - b); 59 | var divideN = (int[] numbers) => numbers.Aggregate(1, (a, b) => a / b); 60 | 61 | log(multiplyN(new[]{1, 2, 3, 4})); 62 | log(addN(new[]{1, 2, 3, 4})); 63 | log(subtractN(new[]{1, 2, 3, 4})); 64 | log(divideN(new[]{1, 2, 3, 4})); 65 | 66 | header("Wrap the functions in a caller:"); 67 | var runCalcs = (int[] values) => { 68 | var fns = new [] { 69 | multiplyN, 70 | addN, 71 | subtractN, 72 | divideN 73 | }; 74 | 75 | foreach (var fn in fns) { 76 | log(fn(values)); 77 | } 78 | }; 79 | 80 | runCalcs(new [] {2, 3, 4, 5}); 81 | 82 | header("Return the results as s dictionary:"); 83 | var runCalcsAsDictionary = (int[] values) => { 84 | return new Dictionary() { 85 | ["multiply"] = Convert.ToString(multiplyN(values)), 86 | ["add"] = Convert.ToString(addN(values)), 87 | ["subtract"] = Convert.ToString(subtractN(values)), 88 | ["divide"] = Convert.ToString(divideN(values)), 89 | }; 90 | }; 91 | 92 | var result = runCalcsAsDictionary(new [] {2, 3, 4, 5}); 93 | 94 | log(System.Text.Json.JsonSerializer.Serialize(result)); 95 | // {"multiply":"120","add":"14","subtract":"-14","divide":"0"} 96 | 97 | header("Return the results as a tuple:"); 98 | var runCalcsAsTuple = (int[] values) => { 99 | return ( 100 | multiplyN(values), 101 | addN(values), 102 | subtractN(values), 103 | divideN(values) 104 | ); 105 | }; 106 | 107 | var ( 108 | multiplyResult, 109 | addResult, 110 | subtractResult, 111 | dividResult 112 | ) = runCalcsAsTuple(new [] {2, 3, 4, 5}); 113 | 114 | log(multiplyResult); 115 | log(addResult); 116 | log(subtractResult); 117 | log(dividResult); 118 | 119 | header("Pick a result dynamically by number of parameters:"); 120 | var callByParamCount = (int[] values) => { 121 | var output = values.Length switch { 122 | 0 => 0, 123 | 1 => values[0], 124 | 2 => values[0] + values[1], 125 | _ => values.Aggregate(0, (a, b) => a + b) * 0.90, // With discount? 126 | }; 127 | 128 | return output; 129 | }; 130 | 131 | log(callByParamCount(new[] {5, 6})); 132 | log(callByParamCount(new int[] {})); 133 | log(callByParamCount(new[] {1, 1, 1, 1})); 134 | 135 | header("Pick a result by dynamically executing a function by number of parameters:"); 136 | var callByParamCountFn = (int[] values) => { 137 | var fn0 = () => 0; 138 | var fnSelf = () => values[0]; 139 | var fnAdd = () => values[0] + values[1]; 140 | var fnAccumulate = () => values.Aggregate(0, (a, b) => a + b); 141 | 142 | var output = values.Length switch { 143 | 0 => fn0(), 144 | 1 => fnSelf(), 145 | 2 => fnAdd(), 146 | _ => fnAccumulate() * 0.90, // With discount? 147 | }; 148 | 149 | return output; 150 | }; 151 | 152 | log(callByParamCountFn(new[] {8, 9})); 153 | log(callByParamCountFn(new int[] {})); 154 | log(callByParamCountFn(new[] {2, 2, 2, 2})); 155 | 156 | header("Dynamically select a function by the length of parameters:"); 157 | var callByParamCountInlineFn = (int[] values) => { 158 | Func fn = values.Length switch { 159 | 0 => () => 0, 160 | 1 => () => values[0], 161 | 2 => () => values[0] + values[1], 162 | _ => () => values.Aggregate(values[0], (a, b) => a + b) * 0.90, // With discount? 163 | }; 164 | 165 | return fn(); 166 | }; 167 | 168 | log(callByParamCountInlineFn(new[] {8, 9})); 169 | log(callByParamCountInlineFn(new int[] {})); 170 | log(callByParamCountInlineFn(new[] {2, 2, 2, 2})); 171 | 172 | header("Select by the type of input"); 173 | int calcByType(T[] values) { 174 | return values[0] switch { 175 | int first => values.Aggregate(0, (a, b) => a + Convert.ToInt32(b)), 176 | string first when Int32.TryParse(first, out var val) => 177 | values.Aggregate(0, (a, b) => a + Convert.ToInt32(b)), 178 | _ => values.Aggregate(0, (a, b) => a + Convert.ToInt32(b)) 179 | }; 180 | }; 181 | 182 | log(calcByType(new [] {"1", "2"})); 183 | log(calcByType(new [] {1, 2})); 184 | 185 | header("Alias the Convert.ToInt32 function:"); 186 | Func intify = (object o) => Convert.ToInt32(o); 187 | 188 | int calcByTypeIntify(T[] values) { 189 | return values[0] switch { 190 | int first => values.Aggregate(0, (a, b) => a + intify(b)), 191 | string first when Int32.TryParse(first, out var val) => 192 | values.Aggregate(0, (a, b) => a + intify(b)), 193 | _ => values.Aggregate(0, (a, b) => a + intify(b)) 194 | }; 195 | }; 196 | 197 | log(calcByTypeIntify(new [] {"1", "2"})); 198 | log(calcByTypeIntify(new [] {1, 2})); -------------------------------------------------------------------------------- /cs-functional/cs-functional.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | cs_functional 7 | enable 8 | enable 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /cs/.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # Tye 66 | .tye/ 67 | 68 | # ASP.NET Scaffolding 69 | ScaffoldingReadMe.txt 70 | 71 | # StyleCop 72 | StyleCopReport.xml 73 | 74 | # Files built by Visual Studio 75 | *_i.c 76 | *_p.c 77 | *_h.h 78 | *.ilk 79 | *.meta 80 | *.obj 81 | *.iobj 82 | *.pch 83 | *.pdb 84 | *.ipdb 85 | *.pgc 86 | *.pgd 87 | *.rsp 88 | *.sbr 89 | *.tlb 90 | *.tli 91 | *.tlh 92 | *.tmp 93 | *.tmp_proj 94 | *_wpftmp.csproj 95 | *.log 96 | *.vspscc 97 | *.vssscc 98 | .builds 99 | *.pidb 100 | *.svclog 101 | *.scc 102 | 103 | # Chutzpah Test files 104 | _Chutzpah* 105 | 106 | # Visual C++ cache files 107 | ipch/ 108 | *.aps 109 | *.ncb 110 | *.opendb 111 | *.opensdf 112 | *.sdf 113 | *.cachefile 114 | *.VC.db 115 | *.VC.VC.opendb 116 | 117 | # Visual Studio profiler 118 | *.psess 119 | *.vsp 120 | *.vspx 121 | *.sap 122 | 123 | # Visual Studio Trace Files 124 | *.e2e 125 | 126 | # TFS 2012 Local Workspace 127 | $tf/ 128 | 129 | # Guidance Automation Toolkit 130 | *.gpState 131 | 132 | # ReSharper is a .NET coding add-in 133 | _ReSharper*/ 134 | *.[Rr]e[Ss]harper 135 | *.DotSettings.user 136 | 137 | # TeamCity is a build add-in 138 | _TeamCity* 139 | 140 | # DotCover is a Code Coverage Tool 141 | *.dotCover 142 | 143 | # AxoCover is a Code Coverage Tool 144 | .axoCover/* 145 | !.axoCover/settings.json 146 | 147 | # Coverlet is a free, cross platform Code Coverage Tool 148 | coverage*.json 149 | coverage*.xml 150 | coverage*.info 151 | 152 | # Visual Studio code coverage results 153 | *.coverage 154 | *.coveragexml 155 | 156 | # NCrunch 157 | _NCrunch_* 158 | .*crunch*.local.xml 159 | nCrunchTemp_* 160 | 161 | # MightyMoose 162 | *.mm.* 163 | AutoTest.Net/ 164 | 165 | # Web workbench (sass) 166 | .sass-cache/ 167 | 168 | # Installshield output folder 169 | [Ee]xpress/ 170 | 171 | # DocProject is a documentation generator add-in 172 | DocProject/buildhelp/ 173 | DocProject/Help/*.HxT 174 | DocProject/Help/*.HxC 175 | DocProject/Help/*.hhc 176 | DocProject/Help/*.hhk 177 | DocProject/Help/*.hhp 178 | DocProject/Help/Html2 179 | DocProject/Help/html 180 | 181 | # Click-Once directory 182 | publish/ 183 | 184 | # Publish Web Output 185 | *.[Pp]ublish.xml 186 | *.azurePubxml 187 | # Note: Comment the next line if you want to checkin your web deploy settings, 188 | # but database connection strings (with potential passwords) will be unencrypted 189 | *.pubxml 190 | *.publishproj 191 | 192 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 193 | # checkin your Azure Web App publish settings, but sensitive information contained 194 | # in these scripts will be unencrypted 195 | PublishScripts/ 196 | 197 | # NuGet Packages 198 | *.nupkg 199 | # NuGet Symbol Packages 200 | *.snupkg 201 | # The packages folder can be ignored because of Package Restore 202 | **/[Pp]ackages/* 203 | # except build/, which is used as an MSBuild target. 204 | !**/[Pp]ackages/build/ 205 | # Uncomment if necessary however generally it will be regenerated when needed 206 | #!**/[Pp]ackages/repositories.config 207 | # NuGet v3's project.json files produces more ignorable files 208 | *.nuget.props 209 | *.nuget.targets 210 | 211 | # Microsoft Azure Build Output 212 | csx/ 213 | *.build.csdef 214 | 215 | # Microsoft Azure Emulator 216 | ecf/ 217 | rcf/ 218 | 219 | # Windows Store app package directories and files 220 | AppPackages/ 221 | BundleArtifacts/ 222 | Package.StoreAssociation.xml 223 | _pkginfo.txt 224 | *.appx 225 | *.appxbundle 226 | *.appxupload 227 | 228 | # Visual Studio cache files 229 | # files ending in .cache can be ignored 230 | *.[Cc]ache 231 | # but keep track of directories ending in .cache 232 | !?*.[Cc]ache/ 233 | 234 | # Others 235 | ClientBin/ 236 | ~$* 237 | *~ 238 | *.dbmdl 239 | *.dbproj.schemaview 240 | *.jfm 241 | *.pfx 242 | *.publishsettings 243 | orleans.codegen.cs 244 | 245 | # Including strong name files can present a security risk 246 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 247 | #*.snk 248 | 249 | # Since there are multiple workflows, uncomment next line to ignore bower_components 250 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 251 | #bower_components/ 252 | 253 | # RIA/Silverlight projects 254 | Generated_Code/ 255 | 256 | # Backup & report files from converting an old project file 257 | # to a newer Visual Studio version. Backup files are not needed, 258 | # because we have git ;-) 259 | _UpgradeReport_Files/ 260 | Backup*/ 261 | UpgradeLog*.XML 262 | UpgradeLog*.htm 263 | ServiceFabricBackup/ 264 | *.rptproj.bak 265 | 266 | # SQL Server files 267 | *.mdf 268 | *.ldf 269 | *.ndf 270 | 271 | # Business Intelligence projects 272 | *.rdl.data 273 | *.bim.layout 274 | *.bim_*.settings 275 | *.rptproj.rsuser 276 | *- [Bb]ackup.rdl 277 | *- [Bb]ackup ([0-9]).rdl 278 | *- [Bb]ackup ([0-9][0-9]).rdl 279 | 280 | # Microsoft Fakes 281 | FakesAssemblies/ 282 | 283 | # GhostDoc plugin setting file 284 | *.GhostDoc.xml 285 | 286 | # Node.js Tools for Visual Studio 287 | .ntvs_analysis.dat 288 | node_modules/ 289 | 290 | # Visual Studio 6 build log 291 | *.plg 292 | 293 | # Visual Studio 6 workspace options file 294 | *.opt 295 | 296 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 297 | *.vbw 298 | 299 | # Visual Studio LightSwitch build output 300 | **/*.HTMLClient/GeneratedArtifacts 301 | **/*.DesktopClient/GeneratedArtifacts 302 | **/*.DesktopClient/ModelManifest.xml 303 | **/*.Server/GeneratedArtifacts 304 | **/*.Server/ModelManifest.xml 305 | _Pvt_Extensions 306 | 307 | # Paket dependency manager 308 | .paket/paket.exe 309 | paket-files/ 310 | 311 | # FAKE - F# Make 312 | .fake/ 313 | 314 | # CodeRush personal settings 315 | .cr/personal 316 | 317 | # Python Tools for Visual Studio (PTVS) 318 | __pycache__/ 319 | *.pyc 320 | 321 | # Cake - Uncomment if you are using it 322 | # tools/** 323 | # !tools/packages.config 324 | 325 | # Tabs Studio 326 | *.tss 327 | 328 | # Telerik's JustMock configuration file 329 | *.jmconfig 330 | 331 | # BizTalk build output 332 | *.btp.cs 333 | *.btm.cs 334 | *.odx.cs 335 | *.xsd.cs 336 | 337 | # OpenCover UI analysis results 338 | OpenCover/ 339 | 340 | # Azure Stream Analytics local run output 341 | ASALocalRun/ 342 | 343 | # MSBuild Binary and Structured Log 344 | *.binlog 345 | 346 | # NVidia Nsight GPU debugger configuration file 347 | *.nvuser 348 | 349 | # MFractors (Xamarin productivity tool) working folder 350 | .mfractor/ 351 | 352 | # Local History for Visual Studio 353 | .localhistory/ 354 | 355 | # BeatPulse healthcheck temp database 356 | healthchecksdb 357 | 358 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 359 | MigrationBackup/ 360 | 361 | # Ionide (cross platform F# VS Code tools) working folder 362 | .ionide/ 363 | 364 | # Fody - auto-generated XML schema 365 | FodyWeavers.xsd 366 | 367 | ## 368 | ## Visual studio for Mac 369 | ## 370 | 371 | 372 | # globs 373 | Makefile.in 374 | *.userprefs 375 | *.usertasks 376 | config.make 377 | config.status 378 | aclocal.m4 379 | install-sh 380 | autom4te.cache/ 381 | *.tar.gz 382 | tarballs/ 383 | test-results/ 384 | 385 | # Mac bundle stuff 386 | *.dmg 387 | *.app 388 | 389 | # content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore 390 | # General 391 | .DS_Store 392 | .AppleDouble 393 | .LSOverride 394 | 395 | # Icon must end with two \r 396 | Icon 397 | 398 | 399 | # Thumbnails 400 | ._* 401 | 402 | # Files that might appear in the root of a volume 403 | .DocumentRevisions-V100 404 | .fseventsd 405 | .Spotlight-V100 406 | .TemporaryItems 407 | .Trashes 408 | .VolumeIcon.icns 409 | .com.apple.timemachine.donotpresent 410 | 411 | # Directories potentially created on remote AFP share 412 | .AppleDB 413 | .AppleDesktop 414 | Network Trash Folder 415 | Temporary Items 416 | .apdisk 417 | 418 | # content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore 419 | # Windows thumbnail cache files 420 | Thumbs.db 421 | ehthumbs.db 422 | ehthumbs_vista.db 423 | 424 | # Dump file 425 | *.stackdump 426 | 427 | # Folder config file 428 | [Dd]esktop.ini 429 | 430 | # Recycle Bin used on file shares 431 | $RECYCLE.BIN/ 432 | 433 | # Windows Installer files 434 | *.cab 435 | *.msi 436 | *.msix 437 | *.msm 438 | *.msp 439 | 440 | # Windows shortcuts 441 | *.lnk 442 | 443 | # JetBrains Rider 444 | .idea/ 445 | *.sln.iml 446 | 447 | ## 448 | ## Visual Studio Code 449 | ## 450 | .vscode/* 451 | !.vscode/settings.json 452 | !.vscode/tasks.json 453 | !.vscode/launch.json 454 | !.vscode/extensions.json 455 | -------------------------------------------------------------------------------- /cs/Program.cs: -------------------------------------------------------------------------------- 1 | var app = new App(); 2 | await app.Run(); 3 | 4 | // * CSharp 5 | record Person ( 6 | string Name, 7 | int Age, 8 | string? Nickname = null 9 | ) { 10 | 11 | public async Task Notify() { 12 | // * Exception Handling 13 | try { 14 | var msg = $"Happy {this.Age}th b-day!"; 15 | return await Task.FromResult(msg); 16 | } 17 | catch (Exception) { throw; } 18 | finally { } 19 | } 20 | 21 | public void Invite( 22 | Action fn, 23 | string[] friends 24 | ) { 25 | foreach (var friend in friends) { 26 | fn(friend); 27 | } 28 | } 29 | } 30 | 31 | 32 | class App { 33 | public async Task Run() { 34 | var amy = new Person("Amy", 20); 35 | // * Async/Await 36 | var message = await amy.Notify(); 37 | Console.WriteLine($"{message}: {amy.Age}"); 38 | // * Destructuring 39 | var (name, age, _) = amy; 40 | // * Array initialization 41 | var friends = new[] { "Anish", "Landry" }; 42 | amy.Invite( 43 | (f) => { 44 | Console.WriteLine($"Invited {f}"); 45 | }, 46 | friends 47 | ); 48 | 49 | void log() { 50 | Console.WriteLine("Completed!"); 51 | } 52 | 53 | log(); 54 | 55 | // * Arrow style 56 | void trace() => Console.WriteLine("Done"); 57 | trace(); 58 | 59 | var thomas = new Person("Thomas", 36); 60 | 61 | var outer = () => { 62 | var ( name, age, _ ) = thomas; 63 | Console.WriteLine(name); 64 | 65 | var inner = string () => { 66 | trace(); 67 | Console.WriteLine("Inner"); 68 | return "Hello from inner()"; 69 | }; 70 | 71 | inner(); 72 | 73 | var local = (string msg) => 74 | Console.WriteLine(msg); 75 | 76 | local("Local!"); 77 | }; 78 | 79 | outer(); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /cs/cs.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /js-battleships/battleships.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The battleships problem requires counting the number of battleships 3 | * on a board. See boards at the end for examples. 4 | * 5 | * - The smallest battleship has size 1. 6 | * - Battleships can only be horizontal or vertical 7 | * - Battleships do not touch 8 | * 9 | * This solution demonstrates an approach to solving it using a recursive 10 | * local function. 11 | */ 12 | 13 | const log = (message) => console.log(message); 14 | 15 | const testCountShips = (board) => { 16 | const width = board.length; 17 | const height = board[0].length; 18 | 19 | log(`Board is ${width} by ${height}`); 20 | 21 | const countShips = (x, y, total) => { 22 | if (x > width - 1 && y < height) { 23 | x = 0; 24 | y++; 25 | } 26 | 27 | if (y > height - 1) { 28 | return total; 29 | } 30 | 31 | if (board[x][y] == 1) { 32 | board[x][y] = 0; 33 | total++; 34 | 35 | for (let i = x + 1; i < width; i++) { 36 | if (board[i][y] == 0) { 37 | x = i - 1; // Skip ahead? 38 | break; 39 | } 40 | 41 | board[i][y] = 0; 42 | } 43 | 44 | for (let i = y + 1; i < height; i++) { 45 | if (board[x][i] == 0) { 46 | break; 47 | } 48 | board[x][i] = 0; 49 | } 50 | } 51 | 52 | // We found a zero; keep going across. 53 | return countShips(x + 1, y, total); 54 | }; 55 | 56 | return countShips(0, 0, 0); 57 | }; 58 | 59 | const expect = (board, expected) => { 60 | const actual = testCountShips(board); 61 | 62 | if (actual !== expected) { 63 | log(`Expected ${expected} but got ${actual}`); 64 | } 65 | else { 66 | log(`Passed with ${expected}`); 67 | } 68 | } 69 | 70 | // Sample boards 71 | const board1 = [ 72 | [1,1,1,0], 73 | [0,0,0,1], 74 | [1,0,0,1], 75 | [1,0,0,0], 76 | ]; 77 | 78 | expect(board1, 3); 79 | 80 | const board2 = [ 81 | [1,1,1,0,1], 82 | [0,0,0,1,0], 83 | [1,0,0,1,0], 84 | [1,0,0,0,0], 85 | [0,0,0,1,1] 86 | ]; 87 | 88 | expect(board2, 5); 89 | 90 | const board3 = [ 91 | [1,0,1,0,1], 92 | [0,1,0,1,0], 93 | [1,0,1,0,1], 94 | [0,1,0,1,0], 95 | [1,0,1,0,1] 96 | ]; 97 | 98 | expect(board3, 13); -------------------------------------------------------------------------------- /js-ts-csharp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CharlieDigital/js-ts-csharp/3dbf37f294139c7ca432c59733ffb2e7ce3344b4/js-ts-csharp.png -------------------------------------------------------------------------------- /js-ts-csharp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.5.002.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "cs", "cs\cs.csproj", "{4662BA7E-32AF-488F-8CC9-CE91D4135F1F}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "cs-battleships", "cs-battleships\cs-battleships.csproj", "{891AA5E0-9295-47B9-96D9-C67A6B04CE0A}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "cs-functional", "cs-functional\cs-functional.csproj", "{2868BF14-AA94-4831-8151-D03AC632BFAB}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Release|Any CPU = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {4662BA7E-32AF-488F-8CC9-CE91D4135F1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {4662BA7E-32AF-488F-8CC9-CE91D4135F1F}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {4662BA7E-32AF-488F-8CC9-CE91D4135F1F}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {4662BA7E-32AF-488F-8CC9-CE91D4135F1F}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {891AA5E0-9295-47B9-96D9-C67A6B04CE0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {891AA5E0-9295-47B9-96D9-C67A6B04CE0A}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {891AA5E0-9295-47B9-96D9-C67A6B04CE0A}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {891AA5E0-9295-47B9-96D9-C67A6B04CE0A}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {2868BF14-AA94-4831-8151-D03AC632BFAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {2868BF14-AA94-4831-8151-D03AC632BFAB}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {2868BF14-AA94-4831-8151-D03AC632BFAB}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {2868BF14-AA94-4831-8151-D03AC632BFAB}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {D29C77C2-4031-4318-A27A-6EC023754D0C} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /js/sample.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | // * JavaScript 5 | class Person { 6 | constructor(name, age, nickname = null) { 7 | this.name = name; 8 | this.age = age; 9 | this.nickname = nickname 10 | } 11 | 12 | async notify() { 13 | // * Exception Handling 14 | try { 15 | var msg = `Happy ${this.age}th b-day!`; 16 | return Promise.resolve(msg); 17 | } 18 | catch (ex) { throw ex; } 19 | finally { } 20 | } 21 | 22 | invite( 23 | fn, 24 | friends 25 | ) { 26 | for (var friend of friends) { 27 | fn(friend); 28 | } 29 | } 30 | } 31 | 32 | class App { 33 | async run() { 34 | var amy = new Person("Amy", 20); 35 | // * Async/Await 36 | var message = await amy.notify(); 37 | console.log(`${message}: ${amy.name}`); 38 | // * Destructuring 39 | var { name, age } = amy; 40 | // * Array initialization 41 | var friends = ["Anish", "Landry"]; 42 | amy.invite( 43 | (f) => { 44 | console.log(`Invited ${f}`) 45 | }, 46 | friends 47 | ); 48 | 49 | function log() { 50 | console.log("Completed!"); 51 | } 52 | 53 | log(); 54 | 55 | // * Arrow style 56 | var trace = () => console.log("Done"); 57 | trace(); 58 | 59 | var thomas = new Person("Thomas", 36); 60 | 61 | var outer = () => { 62 | var { name, age } = thomas; 63 | console.log(name); 64 | 65 | var inner = () => { 66 | trace(); 67 | console.log("Inner"); 68 | return "Hello from inner()"; 69 | } 70 | 71 | inner(); 72 | 73 | var local = (msg) => 74 | console.log(msg); 75 | 76 | local("Local!"); 77 | } 78 | 79 | outer(); 80 | } 81 | } 82 | 83 | var app = new App(); 84 | (async () => await app.run())(); -------------------------------------------------------------------------------- /ts-battleships/battleships.js: -------------------------------------------------------------------------------- 1 | /** 2 | * The battleships problem requires counting the number of battleships 3 | * on a board. See boards at the end for examples. 4 | * 5 | * - The smallest battleship has size 1. 6 | * - Battleships can only be horizontal or vertical 7 | * - Battleships do not touch 8 | * 9 | * This solution demonstrates an approach to solving it using a recursive 10 | * local function. 11 | */ 12 | var log = function (message) { return console.log(message); }; 13 | var testCountShips = function (board) { 14 | var width = board.length; 15 | var height = board[0].length; 16 | log("Board is ".concat(width, " by ").concat(height)); 17 | var countShips = function (x, y, total) { 18 | if (x > width - 1 && y < height) { 19 | x = 0; 20 | y++; 21 | } 22 | if (y > height - 1) { 23 | return total; 24 | } 25 | if (board[x][y] == 1) { 26 | board[x][y] = 0; 27 | total++; 28 | for (var i = x + 1; i < width; i++) { 29 | if (board[i][y] == 0) { 30 | x = i - 1; // Skip ahead? 31 | break; 32 | } 33 | board[i][y] = 0; 34 | } 35 | for (var i = y + 1; i < height; i++) { 36 | if (board[x][i] == 0) { 37 | break; 38 | } 39 | board[x][i] = 0; 40 | } 41 | } 42 | // We found a zero; keep going across. 43 | return countShips(x + 1, y, total); 44 | }; 45 | return countShips(0, 0, 0); 46 | }; 47 | var expect = function (board, expected) { 48 | var actual = testCountShips(board); 49 | if (actual !== expected) { 50 | log("Expected ".concat(expected, " but got ").concat(actual)); 51 | } 52 | else { 53 | log("Passed with ".concat(expected)); 54 | } 55 | }; 56 | // Sample boards 57 | var board1 = [ 58 | [1, 1, 1, 0], 59 | [0, 0, 0, 1], 60 | [1, 0, 0, 1], 61 | [1, 0, 0, 0], 62 | ]; 63 | expect(board1, 3); 64 | var board2 = [ 65 | [1, 1, 1, 0, 1], 66 | [0, 0, 0, 1, 0], 67 | [1, 0, 0, 1, 0], 68 | [1, 0, 0, 0, 0], 69 | [0, 0, 0, 1, 1] 70 | ]; 71 | expect(board2, 5); 72 | var board3 = [ 73 | [1, 0, 1, 0, 1], 74 | [0, 1, 0, 1, 0], 75 | [1, 0, 1, 0, 1], 76 | [0, 1, 0, 1, 0], 77 | [1, 0, 1, 0, 1] 78 | ]; 79 | expect(board3, 13); 80 | -------------------------------------------------------------------------------- /ts-battleships/battleships.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * The battleships problem requires counting the number of battleships 3 | * on a board. See boards at the end for examples. 4 | * 5 | * - The smallest battleship has size 1. 6 | * - Battleships can only be horizontal or vertical 7 | * - Battleships do not touch 8 | * 9 | * This solution demonstrates an approach to solving it using a recursive 10 | * local function. 11 | */ 12 | 13 | const log = (message: any) => console.log(message); 14 | 15 | const testCountShips = (board: number[][]) => { 16 | const width = board.length; 17 | const height = board[0].length; 18 | 19 | log(`Board is ${width} by ${height}`); 20 | 21 | // Just an extra line so things line up :) 22 | 23 | // Inner function closure 24 | const countShips = (x: number, y: number, total: number): number => { 25 | if (x > width - 1 && y < height) { 26 | x = 0; 27 | y++; 28 | } 29 | 30 | if (y > height - 1) { 31 | return total; 32 | } 33 | 34 | if (board[x][y] == 1) { 35 | board[x][y] = 0; 36 | total++; 37 | 38 | for (let i = x + 1; i < width; i++) { 39 | if (board[i][y] == 0) { 40 | x = i - 1; // Skip ahead? 41 | break; 42 | } 43 | 44 | board[i][y] = 0; 45 | } 46 | 47 | for (let i = y + 1; i < height; i++) { 48 | if (board[x][i] == 0) { 49 | break; 50 | } 51 | board[x][i] = 0; 52 | } 53 | } 54 | 55 | // We found a zero; keep going across. 56 | return countShips(x + 1, y, total); 57 | }; 58 | 59 | return countShips(0, 0, 0); 60 | } 61 | 62 | const expect = (board: number[][], expected: number) => { 63 | const actual = testCountShips(board); 64 | 65 | if (actual !== expected) { 66 | log(`Expected ${expected} but got ${actual}`); 67 | } 68 | else { 69 | log(`Passed with ${expected}`); 70 | } 71 | } 72 | 73 | // Sample boards 74 | const board1 = [ 75 | [1,1,1,0], 76 | [0,0,0,1], 77 | [1,0,0,1], 78 | [1,0,0,0], 79 | ]; 80 | 81 | expect(board1, 3); 82 | 83 | const board2 = [ 84 | [1,1,1,0,1], 85 | [0,0,0,1,0], 86 | [1,0,0,1,0], 87 | [1,0,0,0,0], 88 | [0,0,0,1,1] 89 | ]; 90 | 91 | expect(board2, 5); 92 | 93 | const board3 = [ 94 | [1,0,1,0,1], 95 | [0,1,0,1,0], 96 | [1,0,1,0,1], 97 | [0,1,0,1,0], 98 | [1,0,1,0,1] 99 | ]; 100 | 101 | expect(board3, 13); -------------------------------------------------------------------------------- /ts-battleships/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "esnext", 4 | "lib": [ 5 | "DOM", 6 | "ES2015" 7 | ], 8 | /* Modules */ 9 | "module": "es2022", 10 | "esModuleInterop": true, 11 | "forceConsistentCasingInFileNames": true, 12 | "strict": true, 13 | "skipLibCheck": true 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ts/sample.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | // * TypeScript 3 | class Person { 4 | name; 5 | age; 6 | nickname; 7 | constructor(name, age, nickname) { 8 | this.name = name; 9 | this.age = age; 10 | this.nickname = nickname; 11 | } 12 | async notify() { 13 | // * Exception Handling 14 | try { 15 | var msg = `Happy ${this.age}th b-day!`; 16 | return await Promise.resolve(msg); 17 | } 18 | catch (ex) { 19 | throw ex; 20 | } 21 | finally { } 22 | } 23 | invite(fn, friends) { 24 | for (var friend of friends) { 25 | fn(friend); 26 | } 27 | } 28 | } 29 | class App { 30 | async run() { 31 | var amy = new Person("Amy", 20); 32 | // * Async/Await 33 | var message = await amy.notify(); 34 | console.log(`${message}: ${amy.name}`); 35 | // * Destructuring 36 | var { name, age } = amy; 37 | // * Array initialization 38 | var friends = ["Anish", "Landry"]; 39 | amy.invite((f) => { 40 | console.log(`Invited ${f}`); 41 | }, friends); 42 | function log() { 43 | console.log("Completed!"); 44 | } 45 | log(); 46 | // * Arrow style 47 | var trace = () => console.log("Done"); 48 | trace(); 49 | var thomas = new Person("Thomas", 36); 50 | var outer = () => { 51 | var { name, age } = thomas; 52 | console.log(name); 53 | var inner = () => { 54 | trace(); 55 | console.log("Inner"); 56 | return "Hello from inner()"; 57 | }; 58 | inner(); 59 | var local = (msg) => console.log(msg); 60 | local("Local!"); 61 | }; 62 | outer(); 63 | } 64 | } 65 | var app = new App(); 66 | (async () => await app.run())(); 67 | -------------------------------------------------------------------------------- /ts/sample.ts: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | // * TypeScript 5 | class Person { 6 | constructor( 7 | public name: string, 8 | public age: number, 9 | public nickname?: string 10 | ) { } 11 | 12 | async notify(): Promise { 13 | // * Exception Handling 14 | try { 15 | var msg = `Happy ${this.age}th b-day!`; 16 | return await Promise.resolve(msg); 17 | } 18 | catch (ex) { throw ex; } 19 | finally { } 20 | } 21 | 22 | invite( 23 | fn: (friend: string) => void, 24 | friends: string[] 25 | ): void { 26 | for (var friend of friends) { 27 | fn(friend); 28 | } 29 | } 30 | } 31 | 32 | class App { 33 | async run(): Promise { 34 | var amy = new Person("Amy", 20); 35 | // * Async/Await 36 | var message = await amy.notify(); 37 | console.log(`${message}: ${amy.name}`); 38 | // * Destructuring 39 | var { name, age } = amy; 40 | // * Array initialization 41 | var friends = ["Anish", "Landry"]; 42 | amy.invite( 43 | (f) => { 44 | console.log(`Invited ${f}`) 45 | }, 46 | friends 47 | ); 48 | 49 | function log() { 50 | console.log("Completed!"); 51 | } 52 | 53 | log(); 54 | 55 | // * Arrow style 56 | var trace = () => console.log("Done"); 57 | trace(); 58 | 59 | var thomas = new Person("Thomas", 36); 60 | 61 | var outer = () => { 62 | var { name, age } = thomas; 63 | console.log(name); 64 | 65 | var inner = (): string => { 66 | trace(); 67 | console.log("Inner"); 68 | return "Hello from inner()"; 69 | } 70 | 71 | inner(); 72 | 73 | var local = (msg: string) => 74 | console.log(msg); 75 | 76 | local("Local!"); 77 | } 78 | 79 | outer(); 80 | } 81 | } 82 | 83 | var app = new App(); 84 | (async () => await app.run())(); -------------------------------------------------------------------------------- /ts/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "esnext", 4 | "lib": [ 5 | "DOM", 6 | "ES2015" 7 | ], 8 | /* Modules */ 9 | "module": "es2022", 10 | "esModuleInterop": true, 11 | "forceConsistentCasingInFileNames": true, 12 | "strict": true, 13 | "skipLibCheck": true 14 | } 15 | } 16 | --------------------------------------------------------------------------------