Lazy streams in Swift.

Related tags

Video Stream
Overview

Stream

This is a Swift microframework providing a lazy Stream<T> type with generic implementations of ==/!= where T: Equatable.

Streams are lazily-populated as well as lazily-evaluated, making them convenient for procrastinating tasks you don’t want to do yet, like performing an expensive computation in successive stages.

You can construct Streams with SequenceTypes, use them as SequenceTypes, and map and fold to your heart’s content.

Use

Constructing a Stream:

let empty: Stream<Int> = nil
let unary = Stream.pure(4)
let binary = Stream.cons(4, nil)
let fibonacci: Stream<Int> = fix { fib in // fix is from Prelude.framework
	{ x, y in Stream.cons(x + y, fib(y, x + y)) }
}(0, 1)

Note that fibonacci is infinite! Don’t worry about it, just don’t evaluate it all in one go (like with a for loop that never breaks).

It’s safe to extract values from any Stream, whether infinite or not:

let (first, rest) = fibonacci.uncons
let first = fibonacci.first
let rest = fibonacci.rest
let isEmpty = fibonacci.isEmpty

Better yet, use take and drop to do the heavy lifting for you, or map to compute whatever you need to in a new infinite Stream:

// Bad, infinite loops:
for each in fibonacci {}

// Okay, stops:
for each in fibonacci { break }

// Good, doesn’t compute anything til you iterate the result:
let firstFive = fibonacci.take(5)

// Best, doesn’t compute anything til you iterate, and it’s infinite too:
let fibonacciSquares = fibonacci.map { x in x * x }

You can combine Streams together by concatenating them using the ++ operator—even infinite ones:

let aleph = fibonacci ++ fibonacci

This is more useful for prepending elements onto an infinite stream, though:

let fibonacciSquaresForABit = firstFive.map { x in x * x } ++ fibonacci.drop(5)

Full API documentation is in the source.

Integration

  1. Add this repository as a submodule and check out its dependencies, and/or add it to your Cartfile if you’re using carthage to manage your dependencies.
  2. Drag Stream.xcodeproj into your project or workspace, and do the same with its dependencies (i.e. the other .xcodeproj files included in Stream.xcworkspace). NB: Stream.xcworkspace is for standalone development of Stream, while Stream.xcodeproj is for targets using Stream as a dependency.
  3. Link your target against Stream.framework and each of the dependency frameworks.
  4. Application targets should ensure that the framework gets copied into their application bundle. (Framework targets should instead require the application linking them to include Stream and its dependencies.)
You might also like...
Audio player demo based on Swift and SwiftUI, which can play local or network audio.
Audio player demo based on Swift and SwiftUI, which can play local or network audio.

SwiftAudioDemo Audio player demo based on Swift and SwiftUI, which can play local or network audio. In this demo, I have made a radio player to play n

A Swift library to upload video files to api.video platform.
A Swift library to upload video files to api.video platform.

api.video IOS video uploader api.video is the video infrastructure for product builders. Lightning fast video APIs for integrating, scaling, and manag

The front-end of youtube iOS app developed using swift 5
The front-end of youtube iOS app developed using swift 5

Youtube-Front-end-iOS The front-end of youtube iOS app developed using swift 5 UIs 1)-The UI that corresponds to Home Tab bar "The first View " The fi

A high-performance, flexible, and easy-to-use Video compressor library written by Swift.

FYVideoCompressor A high-performance, flexible and easy to use Video compressor library written by Swift. Using hardware-accelerator APIs in AVFoundat

VideoDetect - Video Detect With Swift
VideoDetect - Video Detect With Swift

videoDetect Main flow: start from setUpAssetVideoCapture() Thread 1:setUpAssetVi

DelayStream - A swift version to stream a file or stdin one line at a time with a delay

DelayStream A swift version to stream a file or stdin one line at a time with a

YHPlayer - An easy-to-use video player based on swift language
YHPlayer - An easy-to-use video player based on swift language

YHPlayer An easy-to-use video player based on swift language Features Plays loca

Lazy image loading for SwiftUI

A missing piece in SwiftUI that provides lazy image loading.

LazyHList: Horizontal list with lazy loading with dequeueing

LazyHList LazyVStack and LazyHStack does load content lazly however they do not

Easy background refresh registration, scheduling, execution, and completion. BGTaskScheduler for the lazy.

EasyBackgroundRefresh Easy background refresh registration, scheduling, execution, and completion. BGTaskScheduler for the lazy. Usage For fast refres

Evented I/O streams for Swift
Evented I/O streams for Swift

Noze.io "Das Haus das Verrückte macht." Noze.io is an attempt to carry over the Node.js ideas into pure Swift. It uses libdispatch for event-driven, n

Evented I/O streams for Swift
Evented I/O streams for Swift

Noze.io "Das Haus das Verrückte macht." Noze.io is an attempt to carry over the Node.js ideas into pure Swift. It uses libdispatch for event-driven, n

Streams of values over time
Streams of values over time

Streams of values over time. Tailored for Swift. 🚄 Release Roadmap Getting Started Learn about the Core Reactive Primitives in ReactiveSwift, and Bas

Radio Streams from all over the world. Free and Open.

RadioBrowserKit - The Swift SDK for Radio Browser Radio Streams from all over the world. Free and Open. RadioBrowserKit is a Swift package which lets

A fast, pure swift MongoDB driver based on Swift NIO built for Server Side Swift
A fast, pure swift MongoDB driver based on Swift NIO built for Server Side Swift

A fast, pure swift MongoDB driver based on Swift NIO built for Server Side Swift. It features a great API and a battle-tested core. Supporting both MongoDB in server and embedded environments.

Swift-music - swift-music is a swift package that provides an easy-to-use API for music related developments.

🎼 swift-music Introduction swift-music is a swift package that provides an easy-to-use API for music related developments. Currently available module

Swift-extensions - Swift package extending the Swift programming language.

swift-extensions A package containing extensions for the Swift programming language. Contribution Reporting a bug If you find a bug, please open a bug

BCSwiftTor - Opinionated pure Swift controller for Tor, including full support for Swift 5.5 and Swift Concurrency

BCSwiftTor Opinionated pure Swift controller for Tor, including full support for

Linenoise-Swift  A pure Swift implementation of the Linenoise library. A minimal, zero-config readline replacement.
Linenoise-Swift A pure Swift implementation of the Linenoise library. A minimal, zero-config readline replacement.

Linenoise-Swift A pure Swift implementation of the Linenoise library. A minimal, zero-config readline replacement. Supports Mac OS and Linux Line edit

Comments
  • Initializing Stream with nil generator returns nil.

    Initializing Stream with nil generator returns nil.

    I did following:

        func testNilGenerator() {
            let seq = SequenceOf<Int> {
                GeneratorOf {
                    return nil
                }
            }
            let stream = Stream(seq)
            for s in stream {
                println(s)
            }
        }
    

    Swift compiler does not warning about that. Should it be marked as failable?

    opened by norio-nomura 4
Releases(0.1)
Owner
Antitypical
Antitypical
NextLevelSessionExporter is an export and transcode media library for iOS written in Swift.

NextLevelSessionExporter ?? NextLevelSessionExporter is an export and transcode media library for iOS written in Swift. The library provides customiza

NextLevel 233 Nov 27, 2022
▶️ video player in Swift, simple way to play and stream media on iOS/tvOS

Player Player is a simple iOS video player library written in Swift. Looking for an obj-c video player? Check out PBJVideoPlayer (obj-c). Looking for

patrick piemonte 2k Jan 2, 2023
Player View is a delegated view using AVPlayer of Swift

PlayerView [![CI Status](http://img.shields.io/travis/David Alejandro/PlayerView.svg?style=flat)](https://travis-ci.org/David Alejandro/PlayerView) An

null 131 Oct 25, 2022
A Swift wrapper for the FFmpeg API

SwiftFFmpeg A Swift wrapper for the FFmpeg API. Note: SwiftFFmpeg is still in development, and the API is not guaranteed to be stable. It's subject to

sun_ 376 Nov 24, 2022
YouTubePlayerKit A Swift Package to easily play YouTube videos 📺

A Swift Package to easily play YouTube videos ??

Sven Tiigi 304 Jan 7, 2023
Collection of Swift/iOS-related conference videos. A demo project for SuperArc framework - building modular iOS apps with a µComponent architecture.

SwiftCommunity Beta version is available at TestFlight Collection of Swift/iOS-related conference videos. This project serves as a showcase for the Su

SuperArcSwift 52 Dec 2, 2022
FWVideoPlayer is video Player for iOS in Swift.

FWVideoPlayer Desc FWVideoPlayer is video Player for iOS in Swift. It can play video and audio. You can use it easy. Example To run the example projec

null 1 Oct 15, 2021
Swift Package used for video where I demonstrate how to extract a package to a local framework and modify it.

SegmentedPicker NOTE: This sample code is taken from the article by Frank Jia in his article titled Build a Custom iOS Segmented Control With SwiftUI

Stewart Lynch 1 Oct 11, 2021
360 video player for iOS written in swift - a subset of SceneKit that works

DDDKit An open source library to support 360 videos and pictures. It's designed as a generic 3D library that you can use for much more! Example of use

Guillaume Sabran 123 Aug 9, 2022
Swift library for embedding and controlling YouTube videos in your iOS applications via WKWebView!

YouTubePlayer Embed and control YouTube videos in your iOS applications! Neato, right? Let's see how it works. 0.7.0 Update: WKWebView breaking change

Giles Van Gruisen 825 Jan 3, 2023