List of Listings

Chapter 1. Why Nim?

Listing 1.1. Style insensitivity

Listing 1.2. Style insensitivity and type identifiers

Listing 1.3. Iterating from 0 to 9 in Nim

Listing 1.4. Iterating from 0 to 9 in C++

Listing 1.5. Barking dog modeled using OOP in Python

Listing 1.6. Barking dog modeled using procedural programming in Nim

Listing 1.7. Iterating over a sequence using functional programming in Nim

Listing 1.8. Iterating over a sequence using a procedural style in Nim

Chapter 2. Getting started

Listing 2.1. C

Listing 2.2. Ruby

Listing 2.3. Nim

Listing 2.4. Integer literals

Listing 2.5. Float literals

Listing 2.6. Boolean literals

Listing 2.7. Character literals

Listing 2.8. Constant example

Listing 2.9. Using anonymous procedures

Listing 2.10. Raising an exception

Listing 2.11. The try statements

Listing 2.12. Handling an exception using a try statement

Listing 2.13. Mutable and immutable parameters

Listing 2.14. Enumerator type

Chapter 3. Writing a chat application

Listing 3.1. Conversation between me, John, and Grace about Game of Thrones

Listing 3.2. Typical directory layout for a Nim project

Listing 3.3. Reading command-line parameters

Listing 3.4. Reading from the standard input stream

Listing 3.5. Spawning readLine in a new thread

Listing 3.6. Message type definition and proc stub

Listing 3.7. A representation of a message in JSON

Listing 3.8. Parsing JSON in protocol.nim

Listing 3.9. The mapping between JSON values and the JsonNodeKind type

Listing 3.10. Assigning parsed data to the result variable

Listing 3.11. Testing your new functionality

Listing 3.12. Creating a new message

Listing 3.13. Standard library imports and type definitions

Listing 3.14. Simple Future[int] example

Listing 3.15. Reading files asynchronously in Node

Listing 3.16. Reading files asynchronously in Nim

Listing 3.17. Reading files and writing to them in sequence using await

Listing 3.18. Creating a server socket and accepting connections from clients

Listing 3.19. Creating a new Client instance for each connection

Listing 3.20. Receiving messages from a client

Listing 3.21. The full server implementation so far

Listing 3.22. Sending messages on to other clients

Listing 3.23. The processMessages procedure after listing 3.22 is inserted

Listing 3.24. The client’s connect procedure

Listing 3.25. Reading from standard input asynchronously

Listing 3.26. The final client implementation

Listing 3.27. Conversation between John, Grace, and me about Game of Thrones

Chapter 4. A tour through the standard library

Listing 4.1. Module example.nim

Listing 4.2. Module main.nim

Listing 4.3. Some of the directories in Nim’s configuration file

Listing 4.4. Importing from a subdirectory

Listing 4.5. Module example2.nim

Listing 4.6. Disambiguating identifiers

Listing 4.7. Importing modules into their own namespace

Listing 4.8. Importing only some of the definitions from a module

Listing 4.9. Excluding some definitions when importing

Listing 4.10. Uniform Function Call Syntax

Listing 4.11. Defining a list of integers and strings

Listing 4.12. Creating a hash table

Listing 4.13. Using a custom type as a key in a hash table

Listing 4.14. Defining a hash procedure for custom types

Listing 4.15. Modeling an access list using a HashSet

Listing 4.16. Sorting using the algorithm module

Listing 4.17. The definition of the generic cmp procedure

Listing 4.18. Write "Some Data" to file.txt in the home directory

Listing 4.19. Path-manipulation procedures

Listing 4.20. Displaying the contents of the home directory

Listing 4.21. Using execCmdEx to determine some information about the OS

Listing 4.22. Retrieving command-line arguments using paramStr

Listing 4.23. Parsing the flag using substr

Listing 4.24. Parsing the flag using split

Listing 4.25. Parsing the flag using parseopt

Listing 4.26. Requesting the Nim website using the httpclient module

Chapter 5. Package management

Listing 5.1. MyPkg.nimble

Listing 5.2. The math module

Listing 5.3. The utils module

Listing 5.4. The data module

Listing 5.5. The beginnings of NimbleExample.nimble

Listing 5.6. Using the bigints package in the math module

Listing 5.7. Adding a dependency on the bigints package

Listing 5.8. Commands to upload the NimbleExample package to GitHub

Chapter 6. Parallelism

Listing 6.1. Mutating a global variable using a Thread

Listing 6.2. Passing data to a thread safely

Listing 6.3. Passing multiple values to a thread

Listing 6.4. Executing multiple threads

Listing 6.5. First possible output when the code in listing 6.4 is executed

Listing 6.6. Second possible output when the code in listing 6.4 is executed

Listing 6.7. Executing multiple threads using spawn

Listing 6.8. Reading input from the terminal with spawn

Listing 6.9. Exceptions in a spawned procedure

Listing 6.10. Parsing data with the re module

Listing 6.11. Parsing using split

Listing 6.12. Parsing using parseutils

Listing 6.13. Iterating over each line in a file

Listing 6.14. Parsing each line in a file

Listing 6.15. The output of sequential_counts.nim

Listing 6.16. The finished sequential_counts.nim

Listing 6.17. Output for sequential_counts.nim

Listing 6.18. The top section of parallel_counts.nim

Listing 6.19. The middle section of parallel_counts.nim

Listing 6.20. The last section of parallel_counts.nim

Listing 6.21. Race condition with shared memory

Listing 6.22. Attempting to access a guarded global variable from a thread

Listing 6.23. Incrementing a global variable with a lock

Listing 6.24. Initializing a channel using open

Listing 6.25. Sending and receiving data through a channel

Listing 6.26. parallel_incrementer.nim implemented using channels

Chapter 7. Building a Twitter clone

Listing 7.1. A / route defined using Jester

Listing 7.2. A simple HTTP GET request

Listing 7.3. Tweeter’s directory structure

Listing 7.4. The Tweeter.nimble file

Listing 7.5. The final Tweeter.nimble file

Listing 7.6. A simple Jester test

Listing 7.7. The types that store a Tweeter message and user information

Listing 7.8. Representing the data in figure 7.9 using an instance of Message

Listing 7.9. Representing a user using an instance of User

Listing 7.10. Connecting to a SQLite database

Listing 7.11. Creating tables in a SQLite database

Listing 7.12. The Database type

Listing 7.13. Implementing the post, follow, and create procedures

Listing 7.14. Implementing the findUser and findMessages procedures

Listing 7.15. The setup and close procedures destined for database.nim

Listing 7.16. The new implementation of createDatabase.nim

Listing 7.17. A test of the database module

Listing 7.18. Using a Nim filter to generate HTML

Listing 7.19. Escaping special characters in views/user.nim

Listing 7.20. The second renderUser procedure

Listing 7.21. Final views/user.nim with the new renderMessages procedure

Listing 7.22. Implementing the renderMain procedure

Listing 7.23. The implementation of renderLogin

Listing 7.24. The implementation of renderTimeline

Listing 7.25. The final code of general.nim

Listing 7.26. The /login route

Listing 7.27. The / route

Listing 7.28. The /createMessage route

Listing 7.29. The user route

Listing 7.30. The userLogin procedure

Listing 7.31. The new implementations of the / and user routes

Listing 7.32. The /follow route

Listing 7.33. Configuring Jester

Listing 7.34. NGINX configuration for Jester

Chapter 8. Interfacing with other languages

Listing 8.1. Wrapping time

Listing 8.2. Wrapping struct tm

Listing 8.3. A complete time and localtime wrapper

Listing 8.4. Defining the shared library filename conditionally

Listing 8.5. Wrapping the four necessary types

Listing 8.6. The SDL C prototypes that will be wrapped in this section

Listing 8.7. Wrapping the procedures in the sdl module

Listing 8.8. An SDL application implemented using the sdl wrapper

Listing 8.9. Using the Canvas API in JavaScript

Listing 8.10. Wrapping the Canvas API

Listing 8.11. The index.html file

Listing 8.12. The canvas_test.nim file

Chapter 9. Metaprogramming

Listing 9.1. Modeling the Simple AST shown in figure 9.5

Listing 9.2. A simplified AST for 5 * (5 + 10) displayed using an indentation-based format

Listing 9.3. The Nim AST for 5 * (5 + 10) displayed using an indentation-based format

Listing 9.4. Creating the Nim AST of 5 * (5 + 10)

Listing 9.5. A macro that emits 5 * (5 + 10)

Listing 9.6. A simple config macro

Listing 9.7. The AST of the MyAppConfig type definition

Listing 9.8. Generating the AST for a type definition

Listing 9.9. Converting a list of call statements to a list of nnkIdentDefs nodes

Listing 9.10. The config macro

Listing 9.11. Testing the config macro

Appendix B. Installing Nim

Listing B.1. Hello World

Listing B.2. Compiler output

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.93.141