Programming
Articles, notes, and tutorials filed under Programming.
Latest articles
Showing 1-8 of 17Build an iOS App from Scratch with SwiftUI
Background On Valentine’s day 2021 I put together a small static website, Infinite Love, to hold a handful of pictures. Because it is static, adding a new photo means editing the source and redeploying, not something you want to do from your phone. So I set out to wrap it in an iOS app with a proper “new post” button. I had done plenty of web development, but this was my first time writing anything for iOS. What follows is the walkthrough I wish I’d had: taking an empty Xcode project all the way to an app running on a real iPhone.
Build PyTorch Extensions with CUDA and CFFI
Deprecated warning: PyTorch 1.0+ replaced the old TH/THC tensor backend with ATen, and the torch.utils.ffi toolchain described here was removed along the way. If you target PyTorch 1.0 or newer, write your extensions with torch.utils.cpp_extension instead, see this GitHub repository for a working example. The article below documents the older CFFI-based approach and is kept for reference. Python is one of the most popular languages for deep learning, but as an interpreted language it is slow at tight numerical loops. Most of the time this does not matter, because the heavy lifting happens inside libraries written in C and CUDA. It starts to matter the moment you need an operation that the framework does not provide out of the box.
Lua in a Nutshell: A Quick Tutorial for Lua
I’m reading source code written with Torch these days. Torch is a well-known deep learning framework written in Lua. So I summarize its grammar and provide a quick tutorial here. Getting Started Like many languages, Lua is case-sensitive. The following code outputs “Hello World” with Lua. Note that the semicolon at the end of a line is optional, like JavaScript. print('Hello World') You can use the interpreter of Lua in the command line:
Introduction to Java Virtual Machine
Note: This article has been updated for Java 8 and later. The original 2015 version described the Permanent Generation (PermGen), which Java 8 removed and replaced with Metaspace (JEP 122); the garbage-collection section has also been rewritten to cover the modern HotSpot collectors (G1, ZGC, Shenandoah). The behaviors described here are those of HotSpot; other JVM implementations may differ. What is JVM? The Java Virtual Machine is the cornerstone of the Java platform. It is the component responsible for Java’s hardware and operating-system independence, the small size of its compiled code, and its ability to protect users from malicious programs.
Difference Between require and include in PHP
One of the most common questions I get from PHP beginners is: why are there four different ways to pull one file into another? There is include, include_once, require, and require_once. They look almost interchangeable, but picking the wrong one can either hide a fatal bug or crash a page that should have kept running. The four are really just combinations of two independent choices: include vs require: what happens when the file is missing. include emits a warning and keeps going; require stops the script with a fatal error. plain vs _once: how many times the file may be loaded. The plain form loads the file on every call; the _once form loads it at most once per request. One note before we start: despite the parentheses you often see, include and require are language constructs, not functions. Both require 'core.php'; and require('core.php'); work, but the manual recommends the parenthesis-free form, which is what I’ll use throughout.
Java Collections Framework
Almost all collections in Java are derived from the java.util.Collection interface. Collection defines the basic parts of all collections. The interface states the add() and remove() methods for adding to and removing from a collection respectively. Also required is the toArray() method, which converts the collection into a simple array of all the elements in the collection. Finally, the contains() method checks if a specified element is in the collection. The Collection interface is a subinterface of java.lang.Iterable, so any Collection may be the target of a for-each statement. (The Iterable interface provides the iterator() method used by for-each statements.) Additionally, Collection is a generic, so any collection can be written to store any class. For example, a Collection<String> can hold strings, and the elements can be used as strings without any casting required.
The String Constant Pool
The String Constant Pool is a JVM-wide cache of string literals: when different parts of your code reference a string with the same content, they can share a single String object instead of allocating a new one each time. This is possible only because String is immutable in Java, combined with the String interning mechanism. In other words, the pool is an application of the Flyweight design pattern. String Pool Examples String s1 = "Hello"; String s2 = "Hello"; String s3 = "Hel" + "lo"; String s4 = "Hel" + new String("lo"); String s5 = new String("Hello"); String s6 = s5.intern(); System.out.println(s1 == s2); // true System.out.println(s1 == s3); // true System.out.println(s1 == s4); // false System.out.println(s4 == s5); // false System.out.println(s1 == s6); // true Explanation The references of s1, s2, s3, and s6 all point to the same pooled object, while s4 and s5 do not. Here is why:
The 23 Gang of Three Design Patterns
Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced during software development. These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time. This tutorial will take you through step by step approach and examples using Java while learning Design Pattern concepts. Three Types of Design Patterns Design patterns are divided into three fundamental groups: Behavioral Patterns, Creational Patterns, and Structural Patterns.