Android-Monad


Source link: https://github.com/petitviolet/Android-Monad

Android-Monad

This repository is Yet Another Android-M.

Implemented "Monadic" data structures.

Set up

dependencies {

  compile 'net.petitviolet.android:monad:latest-version' 
}

And, I strongly recommend to use orfjackal/retrolambda together.

How to Use

Maybe

The name of Maybe is derived from Haskell. In Scala, Option.

// with lambda Maybe<Integer> maybeInt = Maybe.of(x);
 maybeInt.flatMap(integer -> Maybe.of(integer % 2 == 0 ? integer : null))

.map(integer -> integer + 5)

.filter(integer -> integer % 3 == 0)

.foreach(integer -> Log.d(TAG, "result: " + integer));
  // without lambda maybeInt.flatMap(new Function.F1<Integer, Maybe<Integer>>() {

  @Override
  public Maybe<Integer> invoke(Integer integer) {

return Maybe.of(integer % 2 == 0 ? integer : null);

  
}
 
}
).map(new Function.F1<Integer, Integer>() {

  @Override
  public Integer invoke(Integer integer) {

return integer + 5;
  
}
 
}
).filter(new Function.F1<Integer, Boolean>() {

  @Override
  public Boolean invoke(Integer integer) {

return integer % 3 == 0;
  
}
 
}
).foreach(new Function.F<Integer>() {

  @Override
  public void invoke(Integer integer) {

Log.d(TAG, "result: " + integer);

  
}
 
}
);

More detailes of Maybe interface is shown in MaybeTest.java.

ListM

Implemented ListM based on ArrayList as a implementation of List interface.

ListM<Integer> listM = ListM.unit();
 listM.addAll(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
 ListM<String> result = listM.filter(i -> i % 3 == 0)

.flatMap(i -> ListM.<Integer>of(i * 10))

.map(s -> s + "!");

.foldRight((a, acc) -> a + ", " + acc, " <= end");
 // result -> 90!, 60!, 30!, <= end

More detailes of ListM interface is shown in ListMTest.java.

State

State monad. Sequencial computations share a state, and each of them uses the state and modify or not, then pass it to next computation.

// state is List<String>, and value is Integer Tuple<Integer, List<String>> result =

State.init((List<String> strings) -> {

 // modify state

 strings.add("nice");

 // value is 1

 return Tuple.of(1, strings);

}
).map(i -> {

 // apply function to value of State

 return i + 10;

}
).map(i -> {

 // apply function to value of State

 return i * 2;

}
).flatMap(integer -> {

 // apply function to value of State

 return State.init(strings -> {

  if (integer % 2 == 0) {

// modify state

strings.add("awesome");

return Tuple.of(integer, strings);

  
}
 else {

// modify state

strings.add("Oops");

return Tuple.of(-100, strings);

  
}

 
}
);

// empty list is given as the first state of above sequencial computations

}
).apply(new ArrayList<>());
 assert result._1 == 22; assert result._2 == List("nice", "awesome");
  // dummy code

Identity

Id monad. Have a value and do nothing. Just monadic.

Identity<String> id = Identity.id("hoge");
 Identity<Integer> result = id.flatMap(new Function.F1<String, Identity<Integer>>() {

@Override

public Identity<Integer> invoke(String s) {

return Identity.id(s.length());

  
}
 
}
);
 assert result.value.equals(4);

Lisence

This code is licensed under the Apache Software License 2.0.

Resources

A lightweight view and resource injection library for android

Loglr is an open source library that enables developers to implement 'Login via Tumblr' with as minimum frustration as possible.

Timeline is a famous and user-friendly feature these days; If you need to implement one of them in your projects, consider using this library.

Android Preferences according to Material design specs.

This library lets the view which is the item in your RecyclerView, or more precisely, the ViewHolder.getItemView() handle the click.

SimpleRESTClientHandler is an Open Source Android library that allows developers to easily make request to a REST server using VOLLEY and GSON.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes