Code Monkey home page Code Monkey logo

defaultmap's Introduction

defaultmap

Build Status codecov Latest Version Rust Documentation GitHub license

It can be useful to not have to worry about missing keys in a map. If a key is requested that doesn't have a value a default value is simply returned. This is exactly what this library provides.

Examples

Counter

A clear use case of this is when counting the unique elements in a list. Here you want to add one to the existing value in the map for that key. This is a problem for the first addition when there's no value for the key yet. With this library you can specify when creating the map that the default value should be zero.

# use defaultmap::*;
let nums = [1, 4, 3, 3, 4, 2, 4];
let mut counts:  DefaultHashMap<i32, i32> = DefaultHashMap::new(0);
// DefaultHashMap::default() is equivalent.

for num in nums.into_iter() {
    counts[*num] += 1;
}

println!("{:?}", counts);
// DefaultHashMap { map: {1: 1, 3: 2, 2: 1, 4: 3}, default: 0 }

# assert_eq!(1, counts[1]);
# assert_eq!(1, counts[2]);
# assert_eq!(2, counts[3]);
# assert_eq!(3, counts[4]);

Synonym lists

Another way the default map can be used is using a map filled with other collections, such as a a Vec, a HashMap or even another default map. Next follows some code to create a map where we start with tuples of synonyms and we end with a map that contains the list of synonyms for each word.

# use defaultmap::*;

let synonym_tuples = [
    ("nice", "sweet"),
    ("sweet", "candy"),
    ("nice", "entertaining"),
    ("nice", "good"),
    ("entertaining", "absorbing"),
];

let mut synonym_map: DefaultHashMap<&str, Vec<&str>> = DefaultHashMap::new(vec![]);
// DefaultHashMap::default() is equivalent.

for &(l, r) in synonym_tuples.into_iter() {
    synonym_map[l].push(r);
    synonym_map[r].push(l);
}

assert_eq!(synonym_map["good"], vec!["nice"]);
assert_eq!(synonym_map["nice"], vec!["sweet", "entertaining", "good"]);
assert_eq!(synonym_map["evil"], Vec::<&str>::new());

defaultmap's People

Contributors

deedasmi avatar dynisious avatar jeltef avatar lukaskalbertodt avatar michaelmior avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.